﻿#include "db_cfg_g_def.h"
#include "DbCfgFun.h"
#include <QString>
#include <QObject>
#include <QMessageBox>
#include <QByteArray>
#include "GetColumnInfo.h"

DbCfgFun::DbCfgFun(void)
{
}

DbCfgFun::~DbCfgFun(void)
{
}

//获取表名表记录根据AppType
int DbCfgFun::GetTableInfobyAppType(int AppType, char *&Buff, int *buff_len)
{
	if (AppType<=0 || Buff != NULL || !buff_len)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	int	Result = 0;
	memset(Sql, 0, SQL_MAX_LEN);
	sprintf(Sql, "select * from ctable_info where AppType=%d order by TableId", AppType);
	Result = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_CD,TABLE_CTABLE_INFO, Sql, Buff, buff_len);
	if (Result<=0)
	{
		unsigned int MySqlErr_Zero = 0;
		unsigned int MySqlErr_One = 0;
		g_pSqLiteDB.GetLastSqlErrNo(MySqlErr_Zero);
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
	}

	return Result;
}

//获取表名表记录根据AppType和表ID
int DbCfgFun::GetTableInfobyAppType_TableId(int AppType, int TableId, char *&Buff, int *buff_len)
{
	if (AppType<=0 || TableId<=0 || Buff != NULL || !buff_len)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	int	Result = 0;
	memset(Sql, 0, SQL_MAX_LEN);
	sprintf(Sql, "select * from ctable_info where AppType='%d' and TableId='%d'", AppType, TableId);
	Result = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_CD,TABLE_CTABLE_INFO, Sql, Buff, buff_len);
	if (Result<=0)
	{
		unsigned int MySqlErr_Zero = 0;
		unsigned int MySqlErr_One = 0;
		g_pSqLiteDB.GetLastSqlErrNo(MySqlErr_Zero);
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
	}

	return Result;
}

//获取表名表记录根据AppType和表ID
int DbCfgFun::GetTableInfobyAppType_TableId(int AppType, int TableId, cTable_Info *pTableInfo)
{
	if (AppType<=0 || TableId<=0 || !pTableInfo)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	int	Result = 0;
	char	*buff = NULL;
	int	buff_len = 0;
	memset(Sql, 0, SQL_MAX_LEN);
	sprintf(Sql, "select * from ctable_info where AppType=%d and TableId=%d", AppType, TableId);
	Result = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_CD,TABLE_CTABLE_INFO, Sql, buff, &buff_len);
	if (Result>0 && buff && buff_len>0 && buff_len == sizeof(cTable_Info))
	{
		memcpy((char*)pTableInfo, buff, buff_len);
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	return Result;
}

int	DbCfgFun::GetPtlColumnInfobyAppType_PtlParamType(int AppType, int PtlParamType, char *&pColumnBuff, int *ColumnNum)
{
	if (AppType <=0 || PtlParamType <=0 || pColumnBuff!= NULL || !ColumnNum)
	{
		return -1;
	}

	*ColumnNum = 0;
	char	sql[SQL_MAX_LEN];
	int	buff_len = 0;
	int	Result = 0;
	sprintf(sql, "select * from cptlcolumn_info where TableId=%d and AppType=%d order by ColumnId", PtlParamType, AppType);
	Result = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_CD,TABLE_CPTLCOLUMN_INFO, sql, pColumnBuff, &buff_len);
	if (Result <= 0)
	{
		if (pColumnBuff)
		{
			delete []pColumnBuff;
			pColumnBuff = NULL;
		}
		return -2;
	}

	*ColumnNum = Result;

	return *ColumnNum;
}

//	获取表的所有域信息AppType和TableId
int DbCfgFun::GetColumnInfobyAppType_TableId(int AppType, int TableId, char *&pColumnBuff, int *ColumnNum)
{
	if (AppType <=0 || TableId <=0 || pColumnBuff!= NULL || !ColumnNum)
	{
		return -1;
	}

	*ColumnNum = 0;
	char	sql[SQL_MAX_LEN];
	int	buff_len = 0;
	int	Result = 0;
	sprintf(sql, "select * from ccolumn_info where TableId=%d and AppType=%d order by ColumnId", TableId, AppType);
	Result = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_CD,TABLE_CCOLUMN_INFO, sql, pColumnBuff, &buff_len);
	if (Result <= 0)
	{
		if (pColumnBuff)
		{
			delete []pColumnBuff;
			pColumnBuff = NULL;
		}
		return -2;
	}

	*ColumnNum = Result;

	return *ColumnNum;
}


//	获取表的当前域的内存偏移
int DbCfgFun::GetCurColumnDataOffset(cColumn_Info *pColumnInfo, int ColumnNum, int coluNo, int &BuffOffset)
{
	BuffOffset = 0;
	if (!pColumnInfo || ColumnNum<=0 )
	{
		return -1;
	}
	
	if (coluNo == 0)
	{
		BuffOffset=0;
		return 1;
	}
	else if (coluNo>0 && coluNo <= ColumnNum)
	{
		for (int i=0; i<coluNo; i++)
		{
			BuffOffset += pColumnInfo[i].clmDataLength;
		}
		return 1;
	}else
	{
		return -2;
	}

	return 0;
}

//	获取表的所有域的内存偏移
int DbCfgFun::GetColBuffOffSetbyColumnInfo(cColumn_Info *pColumnInfo, int ColumnNum, int *&pColBuffOffset)
{
	if (!pColumnInfo || ColumnNum<=0 || pColBuffOffset != NULL)
	{
		return -1;
	}

	int	ColOffset = 0;
	int	Result = 1;
	pColBuffOffset = new int[ColumnNum];
	memset((char*)pColBuffOffset, 0, ColumnNum*sizeof(int));
	for (int i=0; i<ColumnNum; i++)
	{
		Result = GetCurColumnDataOffset(pColumnInfo, ColumnNum, i, ColOffset);
		if (Result>0 && ColOffset>=0)
		{
			pColBuffOffset[i] = ColOffset;
		}else
		{
			break;
		}
	}

	if (Result <= 0)
	{
		if (pColBuffOffset)
		{
			delete [](int*)pColBuffOffset;
			pColBuffOffset = NULL; 
		}
	}

	return Result;
}

//	获取当前表的域名和内存偏移信息
int DbCfgFun::GetCurTableColumnInfo(int AppType, int TableId, char *&pColumnInfo, int *&pDataOffset, int &ColumnNum, int &RecordLen, int PtlParamType)
{
	ColumnNum = 0;
	RecordLen = 0;
	if (AppType<=0 || TableId<=0 || pColumnInfo != NULL || pDataOffset != NULL ) 
	{
		return -1;
	}
	RecordLen = GetTableRecordLen(AppType, TableId);
	if (RecordLen<=0)
	{
		return -2;
	}
	
	int Ret = 0;
	if (PtlParamType>0)
	{
		Ret = GetPtlColumnInfobyAppType_PtlParamType(AppType, PtlParamType, pColumnInfo, &ColumnNum);
	}else
	{
		Ret = GetColumnInfobyAppType_TableId(AppType, TableId, pColumnInfo, &ColumnNum);
	}
	
	if (Ret >0 && ColumnNum >0 && pColumnInfo)
	{
		Ret = GetColBuffOffSetbyColumnInfo((cColumn_Info*)pColumnInfo, ColumnNum, pDataOffset);
		if (Ret>0 && pDataOffset)
		{
			return 1;
		}
	}

	if (pDataOffset)
	{
		delete [](int*)pDataOffset;
		pDataOffset = NULL;
	}
	if (pColumnInfo)
	{
		delete [](char*)pColumnInfo;
		pColumnInfo = NULL;
	}

	return -3;
}

// 获取域的英文名通过AppType、TableId和COLId
int DbCfgFun::GetColEngNamebyAppType_TableId_ColId(int AppType, int TableId, int ColumnId, char *&pColEngName, int *buff_len)
{
	if (AppType <=0 || TableId <=0 || ColumnId<=0 || pColEngName!= NULL || !buff_len)
	{
		return -1;
	}

	*buff_len = 0;
	char	sql[SQL_MAX_LEN];
	int	Result = 0;
	sprintf(sql, "select ColumnEngName from ccolumn_info where TableId=%d and AppType=%d and ColumnId=%d", TableId, AppType, ColumnId);
	Result = g_pSqLiteDB.DB_SelectString(COLUMN_ENG_NAME_LEN, sql, pColEngName, buff_len);
	if (Result <= 0)
	{
		if (pColEngName)
		{
			delete []pColEngName;
			pColEngName = NULL;
		}
		return -2;
	}

	return Result;
}

// 获取域的中文名通过AppType、TableId和COLId
int DbCfgFun::GetColChnNamebyAppType_TableId_ColId(int AppType, int TableId, int ColumnId, char *&pColChnName, int *buff_len)
{
	if (AppType <=0 || TableId <=0 || ColumnId<=0 || pColChnName!= NULL || !buff_len)
	{
		return -1;
	}

	*buff_len = 0;
	char	sql[SQL_MAX_LEN];
	int	Result = 0;
	sprintf(sql, "select ColumnChnName from ccolumn_info where TableId=%d and AppType=%d and ColumnId=%d", TableId, AppType, ColumnId);
	Result = g_pSqLiteDB.DB_SelectString(COLUMN_CHN_NAME_LEN, sql, pColChnName, buff_len);
	if (Result <= 0)
	{
		if (pColChnName)
		{
			delete []pColChnName;
			pColChnName = NULL;
		}
		return -2;
	}

	return Result;
}

// 获取域的中文名通过AppType、TableId和COLId
int DbCfgFun::GetColChnNamebyAppType_TableId_ColId(int AppType, int TableId, int ColumnId, QString &ColNameText)
{
	ColNameText.clear();
	if (AppType <=0 || TableId <=0 || ColumnId<=0 )
	{
		return -1;
	}

	char	sql[SQL_MAX_LEN];
	int	Result = 0;
	char	*buff = NULL;
	int	buff_len = 0;
	sprintf(sql, "select ColumnChnName from ccolumn_info where TableId=%d and AppType=%d and ColumnId=%d", TableId, AppType, ColumnId);
	Result = g_pSqLiteDB.DB_SelectString(COLUMN_CHN_NAME_LEN, sql, buff, &buff_len);
	if (Result > 0 && buff)
	{
		ColNameText = QString::fromLocal8Bit(buff);
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	return Result;
}

//	获取域的数据长度通过AppType、TableId和ColName
int DbCfgFun::GetColLenbyAppType_TableId_ColEnName(int AppType, int TableId, char *pColEnName, int &ColbuffLen)
{
	ColbuffLen=0;
	if (AppType <=0 || TableId <=0 || !pColEnName)
	{
		return -1;
	}

	char	sql[SQL_MAX_LEN];
	int	Result = 0;
	char	*buff = NULL;
	int	buff_len = 0;
	sprintf(sql, "select clmDataLength from ccolumn_info where TableId=%d and AppType=%d and ColumnEngName='%s'", TableId, AppType, pColEnName);
	Result = g_pSqLiteDB.DB_SelectUShort(sql, buff, &buff_len);
	if (Result > 0 && buff)
	{
		memcpy((char*)&ColbuffLen, buff, sizeof(int));
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	return Result;
}

//获取表的英文名，根据AppType和TableId
int DbCfgFun::GetTableEngNamebyAppType_TableId(int AppType, int TableId, char *&Buff, int *buff_len)
{
	if (AppType <=0 || TableId <=0 || Buff != NULL || !buff_len)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	int	Result = 0;
	memset(Sql, 0, SQL_MAX_LEN);
	sprintf(Sql, "select TableEngName from ctable_info where AppType=%d and TableId=%d", AppType, TableId);
	Result = g_pSqLiteDB.DB_SelectString( TABLE_ENG_NAME_LEN, Sql, Buff, buff_len);
	if (Result<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
	}

	return Result;
}

//获取表的英文名，根据AppType和TableId
int DbCfgFun::GetTableEngNamebyAppType_TableId(int AppType, int TableId, char *TableName)
{
	if (AppType <=0 || TableId <=0 || !TableName)
	{
		return -1;
	}
	memset(TableName, 0, TABLE_ENG_NAME_LEN);

	char	Sql[SQL_MAX_LEN];
	char	*buff = NULL;
	int	buff_len = 0;
	int	Result = 0;
	memset(Sql, 0, SQL_MAX_LEN);
	sprintf(Sql, "select TableEngName from ctable_info where AppType=%d and TableId=%d", AppType, TableId);
	Result = g_pSqLiteDB.DB_SelectString( TABLE_ENG_NAME_LEN, Sql, buff, &buff_len);
	if (Result>0 && buff)
	{
		memcpy(TableName, buff, TABLE_ENG_NAME_LEN);
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	return Result;
}

//获取表名及字段名
int DbCfgFun::GetTableEngNameAndColName(int AppType, int TableId, unsigned int ColumnId, char *TableName, char *ColName)
{
	if (AppType<=0 || TableId<=0 || ColumnId<=0 || !TableName || !ColName)
	{
		return -1;
	}
	memset(TableName, 0, TABLE_ENG_NAME_LEN);
	memset(ColName, 0, COLUMN_ENG_NAME_LEN);

	char	*buff = NULL;
	int	buff_len = 0;
	int	Ret = 0;
	Ret = GetTableEngNamebyAppType_TableId(AppType, TableId, buff, &buff_len);
	if (Ret>0 && buff)
	{
		memcpy(TableName, buff, TABLE_ENG_NAME_LEN);
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
	if (Ret<=0)
	{
		return Ret;
	}

	Ret = GetColEngNamebyAppType_TableId_ColId(AppType, TableId, ColumnId, buff, &buff_len);
	if (Ret>0 && buff)
	{
		memcpy(ColName, buff, COLUMN_ENG_NAME_LEN);
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	return Ret;
}

//获取表的中文名，根据AppType和TableId
int DbCfgFun::GetTableChnNamebyAppType_TableId(int AppType, int TableId, char *&Buff, int *buff_len)
{
	if (AppType <=0 || TableId <=0 || Buff != NULL || !buff_len)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	int	Result = 0;
	memset(Sql, 0, SQL_MAX_LEN);
	sprintf(Sql, "select TableChnName from ctable_info where AppType=%d and TableId=%d", AppType, TableId);
	Result = g_pSqLiteDB.DB_SelectString( TABLE_CHN_NAME_LEN, Sql, Buff, buff_len);
	if (Result<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
	}

	return Result;
}

//获取表的中文名，根据AppType和TableId
int DbCfgFun::GetTableChnNamebyAppType_TableId(int AppType, int TableId, QString &TableChnName)
{
	TableChnName.clear();
	if (AppType <=0 || TableId <=0 )
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	int	Result = 0;
	char	*buff = NULL;
	int	buff_len = 0;
	memset(Sql, 0, SQL_MAX_LEN);
	sprintf(Sql, "select TableChnName from ctable_info where AppType=%d and TableId=%d", AppType, TableId);
	Result = g_pSqLiteDB.DB_SelectString( TABLE_ENG_NAME_LEN, Sql, buff, &buff_len);
	if (Result>0 && buff)
	{
		TableChnName = QString::fromLocal8Bit(buff);
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	return Result;
}

//获取AppType和TableId，根据表的英文名
int	DbCfgFun::GetAppType_TableIdbyTableEngName(int &AppType, int &TableId, char *Buff, int Buff_len)
{
	if(Buff==NULL || Buff_len<=0)
	{
		return -1;
	}
	cTable_Info TableInfo_Temp;
	char	Sql[SQL_MAX_LEN];
	int	Result = 0;
	char	*buff = NULL;
	int		buff_len = 0;
	memset(Sql, 0, SQL_MAX_LEN);
	sprintf(Sql, "select * from ctable_info where TableEngName='%s'", Buff);
	Result = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_CD,TABLE_CTABLE_INFO, Sql, buff, &buff_len);
	if (Result>0 && buff && buff_len == sizeof(cTable_Info))
	{
		memcpy((char*)&TableInfo_Temp, buff, buff_len);
		AppType=TableInfo_Temp.AppType;
		TableId=TableInfo_Temp.TableId;
		return 1;
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
		return -2;
	}
	return 0;
}

//获取AppType和TableId，根据表的英文名
int	DbCfgFun::GetAppType_TableIdbyTableEngName(int &AppType, int &TableId, QString TableChnName)
{
	if(TableChnName=="")
	{
		return -1;
	}
	cTable_Info TableInfo_Temp;
	char	Sql[SQL_MAX_LEN];
	int	Result = 0;
	char	*buff = NULL;
	int	buff_len = 0;
	memset(Sql, 0, SQL_MAX_LEN);
	sprintf(Sql, "select * from ctable_info where TableEngName='%s'", TableChnName.toLocal8Bit().data());
	Result = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_CD,TABLE_CTABLE_INFO, Sql, buff, &buff_len);
	if (Result>0 && buff && buff_len == sizeof(cTable_Info))
	{
		memcpy((char*)&TableInfo_Temp, buff, buff_len);
		AppType=TableInfo_Temp.AppType;
		TableId=TableInfo_Temp.TableId;
		return 1;
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
		return -2;
	}
	return 0;
}

//	获取表记录根据TableName
int DbCfgFun::GetRecordBuffbyTableName(char pTablName[], char *&Buff, int *buff_len)
{
	if (!pTablName || Buff != NULL || !buff_len)
	{
		return -1;
	}
	if (QString::fromLocal8Bit(pTablName).length()<=0)
	{
		return -2;
	}
	char	Sql[SQL_MAX_LEN];
	int	EngName_len = 0;
	int	Result = 0;
	QString orderSql;

	int app_type_temp=0;
	int table_id_temp=0;
	QString table_name_temp=QString::fromLocal8Bit(pTablName);
	Result=GetAppType_TableIdbyTableEngName(app_type_temp,table_id_temp,table_name_temp);
	if (Result<=0)
	{
		return -3;
	}
	Result = 0;

	memset(Sql, 0, SQL_MAX_LEN);

	sprintf(Sql, "select * from %s ", pTablName);

	Result = g_pSqLiteDB.DB_SelectByTableId(app_type_temp,table_id_temp, Sql, Buff, buff_len);
	if (Result<=0 || !Buff || buff_len<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
		if (Result>0)
		{	
			Result = 0;
		}
	}

	return Result;
}


//	获取表记录根据表名和parentId
int DbCfgFun::GetRecordBuffbyTableName_ParentId(char pTablName[],int parentId, char *&Buff, int *buff_len)
{
	if (!pTablName || parentId <=0 || Buff != NULL || !buff_len)
	{
		return -1;
	}
	if (QString::fromLocal8Bit(pTablName).length()<=0)
	{
		return -2;
	}
	char	Sql[SQL_MAX_LEN];
	int	EngName_len = 0;
	int	Result = 0;
	QString orderSql;

	int app_type_temp=0;
	int table_id_temp=0;
	QString table_name_temp=QString::fromLocal8Bit(pTablName);
	Result=GetAppType_TableIdbyTableEngName(app_type_temp,table_id_temp,table_name_temp);
	if (Result<=0)
	{
		return -3;
	}
	Result = 0;

	memset(Sql, 0, SQL_MAX_LEN);

	sprintf(Sql, "select * from %s where ParentId=%d", pTablName, parentId);

	Result = g_pSqLiteDB.DB_SelectByTableId(app_type_temp,table_id_temp, Sql, Buff, buff_len);
	if (Result<=0 || !Buff || buff_len<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
		if (Result>0)
		{	
			Result = 0;
		}
	}

	return Result;
}

//获取表记录根据AppType和TableId
int DbCfgFun::GetRecordBuffbyAppType_TableId(int AppType, int TableId, char *&Buff, int *buff_len, bool bOrderBy)
{
	if (AppType<=0 || TableId<=0 || Buff != NULL || !buff_len)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	int	EngName_len = 0;
	int	Result = 0;
	QString orderSql;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	if (bOrderBy)
	{
		Result = GetOrderBySQL_AppTypeTableId(AppType, TableId, orderSql);
	}
	if (orderSql.length()>0)
	{
		sprintf(Sql, "select * from %s %s", pEngName, orderSql.toLocal8Bit().data());
	}else
	{
		sprintf(Sql, "select * from %s ", pEngName);
	}
	Result = g_pSqLiteDB.DB_SelectByTableId(AppType,TableId, Sql, Buff, buff_len);
	if (Result<=0 || !Buff || buff_len<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
		if (Result>0)
		{	
			Result = 0;
		}
	}

	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}

int DbCfgFun::GetRecordBuffbyAppType_TableId_TotalShow(int AppType, int TableId, char *&Buff, int *buff_len, int *RecordLength, bool bOrderBy,int FUN)
{
	if (AppType<=0 || TableId<=0 || Buff != NULL || !buff_len || !RecordLength)
	{
		return -1;
	}
	*buff_len = 0;
	*RecordLength = 0;

	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	int	EngName_len = 0;
	int	Result = 0;
	QString	orderSql;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	if (bOrderBy)
	{
		Result = GetOrderBySQL_AppTypeTableId(AppType, TableId, orderSql);
	}
	if (orderSql.length()>0)
	{

		//遥信
		if(TableId == TABLE_RDEVS_YX )
		{
		  if(FUN == 7)  //开关量普通遥信实际是非4、56
		  {
			  sprintf(Sql, "select %s.* from %s join rDevs_info on (%s.ParentId = rDevs_info.Id+200000) where %s.m_fun !=4 and %s.m_fun !=5 and %s.m_fun !=6 order by rDevs_info.ParentId,%s.m_fun,%s.ParentId,%s.IndexNo",
			pEngName,
			pEngName,
			pEngName,
			pEngName,
			pEngName,
			pEngName,
			pEngName,
			pEngName,
			pEngName);
		  }
		  else 
		  {
			  sprintf(Sql, "select %s.* from %s join rDevs_info on (%s.ParentId = rDevs_info.Id+200000) where %s.m_fun = %d order by rDevs_info.ParentId,%s.m_fun,%s.ParentId,%s.IndexNo" ,
				  pEngName,
				  pEngName,
				  pEngName,
				  pEngName,
				  FUN,
				  pEngName,
				  pEngName,
				  pEngName);
		  
		  }	



		}
		else 
		{ sprintf(Sql, "select %s.* from %s join rDevs_info on (%s.ParentId = rDevs_info.Id+200000) order by rDevs_info.ParentId,%s.ParentId,%s.IndexNo",
			pEngName,
			pEngName,
			pEngName,
			pEngName,
			pEngName);
		}
	}else
	{
		sprintf(Sql, "select * from %s ", pEngName);
	}

	Result = g_pSqLiteDB.DB_SelectByTableId(AppType, TableId, Sql, Buff, buff_len);
	if (Result<=0 || !Buff ||  buff_len<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
		if (Result>0)
		{	
			Result = 0;
		}
	}

	if (Result>0)
	{
		if (0 != (*buff_len)%Result)
		{
			if (Buff)
			{
				delete []Buff;
				Buff = NULL;
			}
			Result = 0;
			*buff_len = 0; 
		}
		if (Result>0)
		{
			*RecordLength = (*buff_len)/Result;
		}
	}

	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}


//获取表记录根据AppType和TableId
int DbCfgFun::GetRecordBuffbyAppType_TableId(int AppType, int TableId, char *&Buff, int *buff_len, int *RecordLength, bool bOrderBy)
{
	if (AppType<=0 || TableId<=0 || Buff != NULL || !buff_len || !RecordLength)
	{
		return -1;
	}
	*buff_len = 0;
	*RecordLength = 0;

	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	int	EngName_len = 0;
	int	Result = 0;
	QString	orderSql;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	if (bOrderBy)
	{
		Result = GetOrderBySQL_AppTypeTableId(AppType, TableId, orderSql);
	}
	if (orderSql.length()>0)
	{
		sprintf(Sql, "select * from %s %s", pEngName, orderSql.toLocal8Bit().data());
	}else
	{
		sprintf(Sql, "select * from %s ", pEngName);
	}

	Result = g_pSqLiteDB.DB_SelectByTableId(AppType, TableId, Sql, Buff, buff_len);
	if (Result<=0 || !Buff ||  buff_len<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
		if (Result>0)
		{	
			Result = 0;
		}
	}
	
	if (Result>0)
	{
		if (0 != (*buff_len)%Result)
		{
			if (Buff)
			{
				delete []Buff;
				Buff = NULL;
			}
			Result = 0;
			*buff_len = 0; 
		}
		if (Result>0)
		{
			*RecordLength = (*buff_len)/Result;
		}
	}

	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}

int	DbCfgFun::GetRecordBuffbyAppType_TableId_TaskId(int AppType, int TableId, int ParentTaskId, char *&Buff, int *buff_len,int *RecordLength)
{
	if (AppType<=0 || TableId<0 || ParentTaskId < 0 || Buff != NULL || !buff_len)
	{
		return -1;
	}
	*RecordLength = 0;
	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	int	EngName_len = 0;
	int	Result = 0;
	QString orderSql;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	Result = GetOrderBySQL_AppTypeTableId(AppType, TableId, orderSql);
	//select %s.* from %s join rDevS_Info on(%s.ParentId = rDevS_Info.Id+%d) where rDevS_Info.ParentId=%d %s
	if (orderSql.length()>0)
	{
		sprintf(Sql, "select %s.* from %s join rDevS_Info on(%s.ParentId = rDevS_Info.Id+%d) where rDevS_Info.ParentId=%d %s", 
				pEngName,
				pEngName,
				pEngName,
				PARENT_OFFSET*TABLE_RDEVS_INFO,
				ParentTaskId,
				orderSql.toLocal8Bit().data());	
	}
	else
	{
		sprintf(Sql, "select %s.* from %s join rDevS_Info on(%s.ParentId = rDevS_Info.Id+%d) where rDevS_Info.ParentId=%d", 
			pEngName,
			pEngName,
			pEngName,
			PARENT_OFFSET*TABLE_RDEVS_INFO,
			ParentTaskId);
	}

	Result = g_pSqLiteDB.DB_SelectByTableId(AppType, TableId, Sql, Buff, buff_len);
	if (Result<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
	}

	if (Result>0)
	{
		if (0 != (*buff_len)%Result)
		{
			if (Buff)
			{
				delete []Buff;
				Buff = NULL;
			}
			Result = 0;
			*buff_len = 0; 
		}
		if (Result>0)
		{
			*RecordLength = (*buff_len)/Result;
		}
	}

	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}

//	获取表记录根据AppType、TableId、parentId
int DbCfgFun::GetRecordBuffbyAppType_TableId_ParentId(int AppType, int TableId, int ParentId, char *&Buff, int *buff_len, bool bOrderBy)
{
	if (AppType<=0 || TableId<0 || /*ParentId < 0 ||*/ Buff != NULL || !buff_len)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	int	EngName_len = 0;
	int	Result = 0;
	QString orderSql;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}
	if (bOrderBy)
	{
		Result = GetOrderBySQL_AppTypeTableId(AppType, TableId, orderSql);
	}
	if (orderSql.length()>0)
	{
		sprintf(Sql, "select * from %s where ParentId=%d %s", pEngName, ParentId, orderSql.toLocal8Bit().data());	
	}else
	{
		sprintf(Sql, "select * from %s where ParentId=%d", pEngName, ParentId);
	}

	Result = g_pSqLiteDB.DB_SelectByTableId(AppType, TableId, Sql, Buff, buff_len);
	if (Result<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
	}
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}

//	获取表记录根据AppType、TableId、parentId
int DbCfgFun::GetRecordBuffbyAppType_TableId_ParentId(int AppType, int TableId, int ParentId, char *&Buff, int *buff_len, int *RecordLength, bool bOrderBy)
{
	if (AppType<=0 || TableId<0 || /*ParentId < 0 ||*/ Buff != NULL || !buff_len || !RecordLength)
	{
		return -1;
	}
	 *buff_len = 0;
	 *RecordLength = 0;
	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	int	EngName_len = 0;
	int	Result = 0;
	QString	orderSql;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	if (bOrderBy)
	{
		Result = GetOrderBySQL_AppTypeTableId(AppType, TableId, orderSql);
	}
	if (orderSql.length()>0)
	{
		sprintf(Sql, "select * from %s where ParentId=%d %s", pEngName, ParentId, orderSql.toLocal8Bit().data());	
	}else
	{
		sprintf(Sql, "select * from %s where ParentId=%d", pEngName, ParentId);
	}
	Result = g_pSqLiteDB.DB_SelectByTableId(AppType, TableId, Sql, Buff, buff_len);
	int aaa=*buff_len;
	if (Result<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
	}
	if (Result>0)
	{
		if (0 != (*buff_len)%Result)
		{
			if (Buff)
			{
				delete []Buff;
				Buff = NULL;
			}
			Result = 0;
			*buff_len = 0; 
		}
		if (Result>0)
		{
			*RecordLength = (*buff_len)/Result;
		}
	}

	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}

//	获取表记录根据AppType、TableId、parentId
int DbCfgFun::GetRecordBuffbyAppType_TableId_sec(int AppType, int TableId, int secId, char *&Buff, int *buff_len, int *RecordLength, bool bOrderBy)
{
	if (AppType<=0 || TableId<0 || secId < 0 || Buff != NULL || !buff_len || !RecordLength)
	{
		return -1;
	}
	*buff_len = 0;
	*RecordLength = 0;
	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	int	EngName_len = 0;
	int	Result = 0;
	QString	orderSql;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	if (bOrderBy)
	{
		Result = GetOrderBySQL_AppTypeTableId(AppType, TableId, orderSql);
	}
	if (orderSql.length()>0)
	{
		sprintf(Sql, "select * from %s where sec=%d %s", pEngName, secId, orderSql.toLocal8Bit().data());	
	}else
	{
		sprintf(Sql, "select * from %s where sec=%d", pEngName, secId);
	}
	Result = g_pSqLiteDB.DB_SelectByTableId(AppType, TableId, Sql, Buff, buff_len);
	int aaa=*buff_len;
	if (Result<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
	}
	if (Result>0)
	{
		if (0 != (*buff_len)%Result)
		{
			if (Buff)
			{
				delete []Buff;
				Buff = NULL;
			}
			Result = 0;
			*buff_len = 0; 
		}
		if (Result>0)
		{
			*RecordLength = (*buff_len)/Result;
		}
	}

	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}


//	获取表记录根据AppType和TableId
int DbCfgFun::GetRecordBuffbyAppType_TableId_RecordId(int AppType, int TableId, int RecordId, char *&Buff, int *buff_len)
{
	if (AppType<=0 || TableId<0 || RecordId <= 0 || Buff != NULL || !buff_len)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	int	EngName_len = 0;
	int	Result = 0;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	sprintf(Sql, "select * from %s where ID=%d", pEngName, RecordId);
	Result = g_pSqLiteDB.DB_SelectByTableId(AppType, TableId, Sql, Buff, buff_len);
	if (Result<=0)
	{
		if (Buff)
		{
			delete []Buff;
			Buff = NULL;
		}
	}
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}


//	获取表记录名称根据AppType、TableId、parentId
int DbCfgFun::GetRecordNamebyAppType_TableId_RecordId(int AppType, int TableId, int RecordId, char *&buff, int *buff_len)
{	
	if (AppType<=0 || TableId<=0 || RecordId<=0 || buff != NULL || !buff_len)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	int	EngName_len = 0;
	int	Result = 0;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	sprintf(Sql, "select Name from %s where ID=%d", pEngName, RecordId);
	Result = g_pSqLiteDB.DB_SelectString( NAME_SIZE, Sql, buff, buff_len);
	if (Result<=0 )
	{
		if (buff)
		{
			delete [](char*)buff;
			buff = NULL;
		}
	}

	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}
	return Result;
}

//	获取表记录名称根据AppType、TableId、parentId
int DbCfgFun::GetRecordNamebyAppType_TableId_RecordId(int AppType, int TableId, int RecordId, QString &RecordName)
{	
	RecordName.clear();

	if (AppType<=0 || TableId<=0 || RecordId<=0 )
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*buff = NULL;
	int	buff_len = 0;
	int	EngName_len = 0;
	int	Result = 0;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	sprintf(Sql, "select Name from %s where ID=%d", pEngName, RecordId);
	Result = g_pSqLiteDB.DB_SelectString( NAME_SIZE, Sql, buff, &buff_len);
	if (Result>0 && buff)
	{
		RecordName = QString::fromLocal8Bit(buff);
	}
	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}
	return Result;
}

//获取表记录条数根据AppType和TableId
int DbCfgFun::GetRecordNumbyAppType_TableId(int AppType, int TableId, int *RecordNum)
{
	if (AppType<=0 || TableId<=0 || !RecordNum)
	{
		return -1;
	}
	*RecordNum = 0;
	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*Buff = NULL;
	int	EngName_len = 0;
	int	buff_len = 0;
	int	Result = 0;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	sprintf(Sql, "select count(*) from %s ", pEngName);
	Result = g_pSqLiteDB.DB_SelectUInt(Sql, Buff, &buff_len);
	if (Result > 0 && Buff && buff_len>0)
	{
		memcpy(RecordNum, Buff, 4);
	}

	if (Buff)
	{
		delete []Buff;
		Buff = NULL;
	}
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}

//获取表记录条数根据AppType和TableId
int DbCfgFun::GetRecordNumbyAppType_TableId(char *TableEnName, int BuffLen, int &RecordNum)
{
	RecordNum = 0;
	if (!TableEnName || BuffLen<TABLE_ENG_NAME_LEN)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	char	*Buff = NULL;
	int	buff_len = 0;
	int	Result = 0;

	int app_type_temp=0;
	int table_id_temp=0;
	QString table_name_temp=QString::fromLocal8Bit(TableEnName);
	Result=GetAppType_TableIdbyTableEngName(app_type_temp,table_id_temp,table_name_temp);
	if (Result<=0)
	{
		return -2;
	}
	Result = 0;

	memset(Sql, 0, SQL_MAX_LEN);
	sprintf(Sql, "select count(*) from %s ", TableEnName);
	Result = g_pSqLiteDB.DB_SelectUInt(Sql, Buff, &buff_len);
	if (Result > 0 && Buff && buff_len>0)
	{
		memcpy(&RecordNum, Buff, 4);
	}

	if (Buff)
	{
		delete []Buff;
		Buff = NULL;
	}

	return Result;
}


//	获取表记录条数根据AppType、TableId和ParentId
int DbCfgFun::GetRecordNumbyAppType_TableId_ParentId(int AppType, int TableId, unsigned int ParentId, int *RecordNum)
{
	if (AppType<=0 || TableId<=0 || !RecordNum || ParentId<=0)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*Buff = NULL;
	int	EngName_len = 0;
	int	buff_len = 0;
	int	Result = 0;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	sprintf(Sql, "select count(*) from %s where ParentId=%d", pEngName, ParentId);
	Result = g_pSqLiteDB.DB_SelectUInt(Sql, Buff, &buff_len);
	if (Result > 0 && Buff && buff_len>0)
	{
		memcpy(RecordNum, Buff, 4);
	}

	if (Buff)
	{
		delete []Buff;
		Buff = NULL;
	}
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}

//	获取记录的ParentId根据AppType、TableId和RecordId
int DbCfgFun::GetParentIdbyAppType_TableId_RecordId(int AppType, int TableId, int RecordId, unsigned int &ParentId)
{
	ParentId = 0;
	if (AppType<=0 || TableId<=0 || RecordId<=0)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*Buff = NULL;
	int	EngName_len = 0;
	int	buff_len = 0;
	int	Result = 0;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return -2;
	}

	sprintf(Sql, "select ParentId from %s where ID=%d", pEngName, RecordId);
	Result = g_pSqLiteDB.DB_SelectUInt(Sql, Buff, &buff_len);
	if (Result > 0 && Buff && buff_len>0)
	{
		memcpy((char*)&ParentId, Buff, 4);
	}

	if (Buff)
	{
		delete []Buff;
		Buff = NULL;
	}
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	return Result;
}

//当前记录是否有子记录
bool DbCfgFun::CurRecordIsHasChild(int CurAppType, int CurTableId, unsigned int RecordId, int ChildTableId)
{
	if (CurAppType<=0 || CurTableId<=0 || RecordId<=0 || ChildTableId<=0 )
	{
		return false;
	}

	char	 sql[SQL_MAX_LEN];
	int	 Result = 0;
	int	 Count = 0;
	char	 *buff = NULL;
	int	 buff_len =0;
	char	 TableName[TABLE_ENG_NAME_LEN];
	memset(sql, 0, SQL_MAX_LEN);
	memset(TableName, 0, TABLE_ENG_NAME_LEN);

	if (GetTableEngNamebyAppType_TableId(CurAppType,ChildTableId,TableName)<=0)
	{
		return false;
	}

	sprintf(sql, "select count(*) from %s where ParentId=%d ",TableName, RecordId+PARENT_OFFSET*CurTableId);
	Result = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (Result > 0 && buff && buff_len>0)
	{
		memcpy(&Count, buff, sizeof(int));
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
	if (Count>0)
	{
		return true;
	}

	return false;
}

//	获取创建表的SQL语句
int DbCfgFun::GetCreateTableSQL(int AppType, int TableID, char *sql, char *TableName)
{
	if (!sql || !TableName || AppType<=0 || TableID<=0)
		return -1;

	QString tablename = QString::fromLocal8Bit(TableName);
	if (tablename.length()<=0)
	{
		return -1;
	}
	QString		str;
	cColumn_Info *pColumnInfo = NULL;
	char		 *buff = NULL;
	int			 ColumNum = 0;	
	int		 Ret1 = 0;
	str.clear();
	str = QString::fromLocal8Bit("create table if not exists %1 (").arg(QString::fromLocal8Bit(TableName));

	Ret1 = GetColumnInfobyAppType_TableId(AppType, TableID, buff, &ColumNum);
	if (Ret1<0 || ColumNum <= 0 || !buff)
	{
		if (buff)
		{
			delete [](char*)buff;
			buff = NULL;
		}
		return Ret1;
	}

	pColumnInfo = (cColumn_Info *)buff;

	int				KeyNum = 0;
	QString			value;
	bool			bHasAutoIncrement = false;
	for(int i=0; i<ColumNum; i++)
	{
		//域名
		value.clear();

		value = QString::fromLocal8Bit("%1 ").arg(QString::fromLocal8Bit(pColumnInfo[i].ColumnEngName));
		str += value;

		//域数据类型
		value.clear();
		switch(pColumnInfo[i].clmDataType)
		{
		case DATA_TYPE_CHAR:
			{
				value = QString::fromLocal8Bit("char(%1) not null ").arg(pColumnInfo[i].clmDataLength);
			}
			break;
		case DATA_TYPE_BYTE:
		case DATA_TYPE_UCHAR:
		case DATA_TYPE_UBYTE:
			{
				value = QString::fromLocal8Bit("tinyint not null ");
			}
			break;
		case DATA_TYPE_SHORT:
		case DATA_TYPE_USHORT:
			{
				value = QString::fromLocal8Bit("smallint not null ");
			}
			break;
		case DATA_TYPE_INT:
		case DATA_TYPE_UINT:
			{
				value = QString::fromLocal8Bit("int not null ");
			}
			break;
		case DATA_TYPE_LONGLONG:
			{
				value = QString::fromLocal8Bit("bigint not null ");
			}
			break;
		case DATA_TYPE_FLOAT:
		case DATA_TYPE_DOUBLE:
			{
				value = QString::fromLocal8Bit("real not null ");
			}
			break;
		case DATA_TYPE_VARBINARY:
			{
				value = QString::fromLocal8Bit("blob ");
			}
			break;
		default:
			break;
		}

		//判断关键字个数
		if (pColumnInfo[i].clmKeyIndex > 0)
		{
			KeyNum++;
		}
		if (ColumNum-1 == i)
		{	
			str=str+value;	
		}else
		{
			str=str+value+QString::fromLocal8Bit(",");	
		}

	}

	//设置关键字
	value.clear();
	if (KeyNum>0)
	{
		value = QString::fromLocal8Bit(", primary key (");
		str += value;

		for (int j=0; j<ColumNum && KeyNum>0; j++)
		{
			if (pColumnInfo[j].clmKeyIndex > 0)
			{
				value.clear();
				if (KeyNum > 1)
				{
					value = QString::fromLocal8Bit("%1,").arg(QString::fromLocal8Bit(pColumnInfo[j].ColumnEngName));
				}else
				{
					value = QString::fromLocal8Bit("%1)").arg(QString::fromLocal8Bit(pColumnInfo[j].ColumnEngName));
				}	

				str += value;
				KeyNum -= 1;
			}
		}
	}

	//SQL 语句结束 
	str += QString::fromLocal8Bit(")");
	if (buff)
	{
		pColumnInfo = NULL;
		delete [](char *)buff;
		buff=NULL;
	}

	int length = str.length();
	if (str.length() <= 5*SQL_MAX_LEN)
	{
		sprintf(sql, "%s", str.toLocal8Bit().data());
	}else
	{
		return -1;
	}

	return 1;
}

//	获取创建表的SQL语句
int DbCfgFun::GetCreateTableSQL(int AppType, int TableID, char *sql)
{
	if (!sql || AppType<=0 || TableID<=0)
		return -1;

	QString		str;
	char		TabName[TABLE_ENG_NAME_LEN];
	cColumn_Info *pColumnInfo = NULL;
	char		 *buff = NULL;
	int		 ColumNum = 0;	
	int		 Ret1 = 0;
	//获取表名
	memset(TabName, 0, TABLE_ENG_NAME_LEN);
	int Ret = GetTableEngNamebyAppType_TableId(AppType, TableID, TabName);
	if (Ret<= 0)
	{
		return -1;
	}
	str.clear();
	str = QString::fromLocal8Bit("create table if not exists %1 (").arg(QString::fromLocal8Bit(TabName));

	Ret1 = GetColumnInfobyAppType_TableId(AppType, TableID, buff, &ColumNum);
	if (Ret1<0 || ColumNum <= 0 || !buff)
	{
		if (buff)
		{
			delete [](char*)buff;
			buff = NULL;
		}
		return Ret1;
	}

	pColumnInfo = (cColumn_Info *)buff;

	int			KeyNum = 0;
	QString			value;
	bool			bHasAutoIncrement = false;
	for(int i=0; i<ColumNum; i++)
	{
		//域名
		value.clear();

		value = QString::fromLocal8Bit("%1 ").arg(QString::fromLocal8Bit(pColumnInfo[i].ColumnEngName));
		str += value;

		//域数据类型
		value.clear();
		switch(pColumnInfo[i].clmDataType)
		{
		case DATA_TYPE_CHAR:
			{
				value = QString::fromLocal8Bit("char(%1) not null ").arg(pColumnInfo[i].clmDataLength);
			}
			break;
		case DATA_TYPE_BYTE:
			{
				value = QString::fromLocal8Bit("tinyint(4) not null ");
			}
			break;
		case DATA_TYPE_SHORT:
			{
				if (SYS_STATE_VALID == pColumnInfo[i].clmIsMark)
				{
					value = QString::fromLocal8Bit("smallint(6) not null auto_increment");
				}else
				{
					value = QString::fromLocal8Bit("smallint(6) not null ");
				}
			}
			break;
		case DATA_TYPE_INT:
			{
				if (SYS_STATE_VALID == pColumnInfo[i].clmIsMark)
				{
					value = QString::fromLocal8Bit("int(11) not null auto_increment");
					bHasAutoIncrement = true;
				}else
				{
					value = QString::fromLocal8Bit("int(11) not null ");
				}
			}
			break;
		case DATA_TYPE_LONGLONG:
			{
				value = QString::fromLocal8Bit("bigint(20) not null ");
			}
			break;
		case DATA_TYPE_FLOAT:
			{
				value = QString::fromLocal8Bit("float not null ");
			}
			break;
		case DATA_TYPE_DOUBLE:
			{
				value = QString::fromLocal8Bit("double not null ");
			}
			break;
		case DATA_TYPE_UCHAR:
		case DATA_TYPE_UBYTE:
			{
				value = QString::fromLocal8Bit("tinyint(3) unsigned not null ");
			}
			break;
		case DATA_TYPE_USHORT:
			{
				if (SYS_STATE_VALID == pColumnInfo[i].clmIsMark)
				{
					value = QString::fromLocal8Bit("smallint(5) unsigned not null auto_increment");
				}else
				{
					value = QString::fromLocal8Bit("smallint(5) unsigned not null ");
				}
			}
			break;
		case DATA_TYPE_UINT:
			{
				if (SYS_STATE_VALID == pColumnInfo[i].clmIsMark)
				{
					value = QString::fromLocal8Bit("int(10) unsigned not null auto_increment");
				}else
				{
					value = QString::fromLocal8Bit("int(10) unsigned not null ");
				}
			}
			break;
		case DATA_TYPE_VARBINARY:
			{
				value = QString::fromLocal8Bit("blob ");
			}
			break;
		default:
			break;
		}

		//判断关键字个数
		if (pColumnInfo[i].clmKeyIndex > 0)
		{
			KeyNum++;
		}
		str=str+value+QString::fromLocal8Bit(",");			
	}

	//设置关键字
	value.clear();
	if (KeyNum>0)
	{
		value = QString::fromLocal8Bit(" primary key (");
		str += value;

		for (int j=0; j<ColumNum && KeyNum>0; j++)
		{
			if (pColumnInfo[j].clmKeyIndex > 0)
			{
				value.clear();
				if (KeyNum > 1)
				{
					value = QString::fromLocal8Bit("%1,").arg(QString::fromLocal8Bit(pColumnInfo[j].ColumnEngName));
				}else
				{
					value = QString::fromLocal8Bit("%1)").arg(QString::fromLocal8Bit(pColumnInfo[j].ColumnEngName));
				}	

				str += value;
				KeyNum -= 1;
			}
		}
	}

	if (buff)
	{
		pColumnInfo = NULL;
		delete [](char *)buff;
		buff=NULL;
	}

	//SQL语句结束
	if (bHasAutoIncrement)
	{	
		value = QString::fromLocal8Bit(") auto_increment=1, type = myisam ");
	}else
	{
		value = QString::fromLocal8Bit(") type = myisam ");
	}

	str += value;

	if (str.length() <= 3*SQL_MAX_LEN)
	{
		sprintf(sql, "%s", str.toLocal8Bit().data());
	}else
	{
		return -1;
	}
	return 1;
}

//	当前遥控是否使用在遥信中（主遥控、副遥控 不重复） 
bool DbCfgFun::CurYKIsUsedInYx(unsigned int YkId)
{	
	if (YkId<=0)
	{
		return true;
	}

	char	sql[SQL_MAX_LEN];
	char	*buff = NULL;
	int	buff_len = 0;
	int	RetNum = 0;
	int	Count = 0;
	unsigned int	RltYkId = 0;
	memset(sql, 0, SQL_MAX_LEN);
	RltYkId = YkId+TABLE_RDEVS_YK*PARENT_OFFSET;
	
	sprintf(sql, "select count(*) from rDevS_Yk where RelYkID=%d", RltYkId);
	
	RetNum = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (RetNum>0 && buff && buff_len>0)
	{
		memcpy(&Count, buff, sizeof(int));
	}

	if (buff)
	{
		delete [](char *)buff;
		buff = NULL;
	}

	if (Count >0)
	{
		return true;
	}

	return false;
}

//当前记录是否已关联使用在某表的某个字段
//输入：记录ID，记录所在表，要查询的表的字段ID
bool DbCfgFun::CurRecordIsUsedInColRelate(unsigned int RecordId, unsigned int RltTableId, char *pTableName, char *pColName)
{
	if (RecordId <=0 || !pTableName || !pColName || RltTableId<=0)
	{
		return false;
	}

	char	Sql[SQL_MAX_LEN];
	char	*Buff = NULL;
	int	buff_len = 0;
	int	Result = 0;
	int	Count = 0;

	int app_type_temp=0;
	int table_id_temp=0;
	QString table_name_temp=QString::fromLocal8Bit(pTableName);
	Result=GetAppType_TableIdbyTableEngName(app_type_temp,table_id_temp,table_name_temp);
	if (Result<=0)
	{
		return false;
	}
	Result = 0;

	memset(Sql, 0, SQL_MAX_LEN);
	
	sprintf(Sql, "select count(*) from %s where %s=%d", pTableName, pColName, RecordId+PARENT_OFFSET*RltTableId);
	Result = g_pSqLiteDB.DB_SelectUInt(Sql, Buff, &buff_len);
	if (Result > 0 && Buff && buff_len>0)
	{
		memcpy(&Count, Buff, sizeof(int));
	}

	if (Buff)
	{
		delete []Buff;
		Buff = NULL;
	}
	
	if (Count>0)
	{
		return true;
	}else
	{
		return false;
	}
}

//当前记录是否已关联使用在某表的某个字段
//输入：记录ID，记录所在表，要查询的表的字段ID
bool DbCfgFun::CurRecordIsUsedInColRelate(int AppType, unsigned int RecordId, int RecordTableId, int CheckTableId, int CheckColId)
{
	if (AppType<=0 || RecordId <=0 || RecordTableId<=0 || CheckTableId<=0 || CheckColId<=0)
	{
		return true;
	}
	
	char	Sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*pColName = NULL;
	char	*Buff = NULL;
	int	EngName_len = 0;
	int	ColName_len = 0;
	int	buff_len = 0;
	int	Result = 0;
	int	Count = 0;

	memset(Sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, CheckTableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}

		return true;
	}

	Result = GetColEngNamebyAppType_TableId_ColId(AppType, CheckTableId, CheckColId, pColName, &ColName_len);
	if (Result<=0 || !pColName || ColName_len<=0)
	{
		if (pColName)
		{
			delete [](char*)pColName;
			pColName = NULL;
		}
		return true;
	}

	sprintf(Sql, "select count(*) from %s where %s=%d", pEngName, pColName, RecordId+PARENT_OFFSET*RecordTableId);
	Result = g_pSqLiteDB.DB_SelectUInt(Sql, Buff, &buff_len);
	if (Result > 0 && Buff && buff_len>0)
	{
		memcpy(&Count, Buff, sizeof(int));
	}

	if (Buff)
	{
		delete []Buff;
		Buff = NULL;
	}
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}
	if (pColName)
	{
		delete []pColName;
		pColName = NULL;
	}
	if (Count>0)
	{
		return true;
	}else
	{
		return false;
	}
}

//	获取当前表的最大空闲ID（unsigned int类型，加1顺序递增）
int DbCfgFun::GetMaxFreeId(unsigned int AppType, unsigned int TableId, unsigned int &RecordId)
{
	RecordId = 0;
	if (AppType<=0 || TableId<=0)
	{
		return -1;
	}
	char	sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*buff = NULL;
	int	Name_len = 0;
	int	buff_len = 0;
//	int	RecordCount = 0;
	int	Result = 0;
	memset(sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &Name_len);
	if (Result <=0 || !pEngName || Name_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}
		return -2;
	}

	sprintf(sql, "select max(ID) from %s ", pEngName);
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}
	Result = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (Result>0 && buff && buff_len>0)
	{
		memcpy(&RecordId, buff, sizeof(unsigned int));
		if (RecordId<=0)
		{
			RecordId = 1;
		}else
		{
			RecordId += 1;			
		}

	}else if (0 ==Result)
	{
		RecordId = 1;
		Result = 1;
	}

	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	return Result;
}

//	获取当前表的空闲ID（unsigned int类型，加1顺序递增）
int DbCfgFun::GetNewFreeId(int AppType, int TableId, unsigned int &RecordId,unsigned int TypeValue)
{
	RecordId = 0;
	if (AppType<=0 || TableId<=0)
	{
		return -1;
	}
	if (SYSTEM_TYPE_NSA_CD == AppType)
	{
		return -2;
	}

	int Result = 0;
	if (TABLE_RDEVS_YX == TableId || 
		TABLE_RDEVS_YC == TableId ||
		TABLE_RDEVS_YK == TableId ||
		TABLE_RDEVS_YM == TableId ||
		TABLE_RDEVS_RYB == TableId || 
		TABLE_RDEVS_BHYC == TableId ||
		TABLE_RTRANSMIT_YX == TableId ||
		TABLE_RTRANSMIT_YC == TableId ||
		TABLE_RTRANSMIT_YM == TableId ||
		TABLE_RTRANSMIT_YK == TableId ||
		TABLE_RDEVMODEL_YX == TableId ||
		TABLE_RDEVMODEL_YC == TableId ||
		TABLE_RDEVMODEL_YM == TableId ||
		TABLE_RDEVMODEL_YK == TableId ||
		TABLE_RDEVMODEL_BHYC == TableId || 
		TABLE_RDEVMODEL_SET == TableId ||
		TABLE_RDEVMODEL_PARA == TableId ||
		TABLE_RDEVMODEL_PREC == TableId ||
		TABLE_RDEVMODEL_RYB == TableId)
	{
		Result = GetMaxFreeId(AppType, TableId, RecordId);
		if (0 < RecordId && RecordId < PARENT_OFFSET)   
		{
			return 1;
		}
	}

	char	sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*buff = NULL;
	int	Name_len = 0;
	int	buff_len = 0;
//	int	RecordCount = 0;
	//int	Result = 0;
	int	IDNum = 0;
	bool	bGetNewId = false;
	memset(sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &Name_len);
	if (Result <=0 || !pEngName || Name_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}
		return -2;
	}

	if (TypeValue>0)
	{
		sprintf(sql, "select ID from %s where ID>%d and ID<%d order by ID", pEngName, TypeValue*1000,(TypeValue+1)*1000);
	}else
	{
	sprintf(sql, "select ID from %s order by ID", pEngName);
	}
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	IDNum = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (IDNum>0 && buff && buff_len>0)
	{
		if (IDNum*sizeof(unsigned int) != buff_len)
		{
			if (buff)
			{
				delete [](char*)buff;
				buff = NULL;
			}
			IDNum = -1;
		}
	}else if (IDNum == 0)
	{
		IDNum = 1;
		if (TypeValue>0)
		{
			RecordId = TypeValue*1000+1;
		}else
		{
		RecordId = 1;
		}
	}

	if (buff && IDNum>0)
	{
		if (TypeValue>0 && ((unsigned int*)buff)[0]>TypeValue*1000+1)
		{
			RecordId = TypeValue*1000+1;
		}else if (TypeValue<=0 && ((unsigned int*)buff)[0]>1)
		{
			RecordId = 1;
		}else
		{
			bGetNewId = false;
			for (int i=0; i<IDNum-1; i++)
			{
				if ( (((unsigned int*)buff)[i+1]-((unsigned int*)buff)[i]) > 1)
				{
					RecordId = ((unsigned int*)buff)[i]+1;
					bGetNewId = true;
					break;
				}
			}

			if (!bGetNewId)
			{
				RecordId = ((unsigned int*)buff)[IDNum-1]+1;
			}
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	if (TypeValue>0 && RecordId >= (TypeValue+1)*1000)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("记录ID已超过该类型[%1]最大ID限制，表ID[%2], 应用类型[%3],").arg(TypeValue).arg(TableId).arg(AppType));
		RecordId = 0;
		return -3;
	}
	if (RecordId>=PARENT_OFFSET)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("记录ID已超过最大ID限制，表ID[%1], 应用类型[%2]").arg(TableId).arg(AppType));
		RecordId = 0;
		return -3;
	}
	return IDNum;
}

//设置记录默认值
int DbCfgFun::SetDefaultData(char *pRecordBuff, cColumn_Info *pColumnInfo, int *pDataOffset, int ColumnNum, int RecordLen, unsigned int ParentId)
{
	if (!pRecordBuff || !pColumnInfo || !pDataOffset || ColumnNum<=0 || RecordLen<=0)
	{
		return -1;
	}
	memset(pRecordBuff, 0, RecordLen);
	for (int i=0; i<ColumnNum; i++)
	{
		if (pDataOffset[i]<0 || pDataOffset[i] >= RecordLen)
		{
			continue;
		}
		if (pColumnInfo[i].ColumnId > ColumnNum)
		{
			break;
		}

		if (0 == QString::fromLocal8Bit(pColumnInfo[i].ColumnEngName).compare("ID", Qt::CaseInsensitive) )
		{
			continue;
		}
		if (0 == QString::fromLocal8Bit(pColumnInfo[i].ColumnEngName).compare("ParentId", Qt::CaseInsensitive))
		{
			memcpy(pRecordBuff+pDataOffset[i], (char*)&ParentId, pColumnInfo[i].clmDataLength);
			continue;
		}
// 		if (TABLE_RSETPARA_INFO == pColumnInfo[i].TableId && 
// 			0 == QString::fromLocal8Bit(pColumnInfo[i].ColumnEngName).compare("sec", Qt::CaseInsensitive))
// 		{
// 			memcpy(pRecordBuff+pDataOffset[i], (char*)&ParentId, pColumnInfo[i].clmDataLength);
// 			continue;
// 		}

		switch (pColumnInfo[i].clmDataType)
		{
		case DATA_TYPE_BYTE:
			{
				char	DataValue = 0;
				DataValue = (char)pColumnInfo[i].clmDefaultValue;
				memcpy(pRecordBuff+pDataOffset[i], (char*)&DataValue, pColumnInfo[i].clmDataLength);
			}
			break;
		case DATA_TYPE_SHORT:
			{
				short	DataValue = 0;
				DataValue = (short)pColumnInfo[i].clmDefaultValue;
				memcpy(pRecordBuff+pDataOffset[i], (char*)&DataValue, pColumnInfo[i].clmDataLength);
			}
			break;
		case DATA_TYPE_INT:
			{
				int	DataValue = 0;
				DataValue = (int)pColumnInfo[i].clmDefaultValue;
				memcpy(pRecordBuff+pDataOffset[i], (char*)&DataValue, pColumnInfo[i].clmDataLength);
			}
			break;
		case DATA_TYPE_FLOAT:
			{
				float	DataValue = pColumnInfo[i].clmDefaultValue;
				memcpy(pRecordBuff+pDataOffset[i], (char*)&DataValue, pColumnInfo[i].clmDataLength);
			}
			break;
		case DATA_TYPE_DOUBLE:
			{
				double	DataValue = pColumnInfo[i].clmDefaultValue;
				memcpy(pRecordBuff+pDataOffset[i], (char*)&DataValue, pColumnInfo[i].clmDataLength);
			}
			break;
		case DATA_TYPE_LONGLONG:
			{
				qint64	DataValue = pColumnInfo[i].clmDefaultValue;
				memcpy(pRecordBuff+pDataOffset[i], (char*)&DataValue, pColumnInfo[i].clmDataLength);
			}
			break;
		case DATA_TYPE_UCHAR:
		case DATA_TYPE_UBYTE:
			{
				unsigned char	DataValue = pColumnInfo[i].clmDefaultValue;
				memcpy(pRecordBuff+pDataOffset[i], (char*)&DataValue, pColumnInfo[i].clmDataLength);
			}
			break;
		case DATA_TYPE_USHORT:
			{
				unsigned short	DataValue = pColumnInfo[i].clmDefaultValue;
				memcpy(pRecordBuff+pDataOffset[i], (char*)&DataValue, pColumnInfo[i].clmDataLength);
			}	
			break;
		default:
			{
				memset(pRecordBuff+pDataOffset[i], 0, pColumnInfo[i].clmDataLength);
			}
			break;
		}
	}
	
	return 1;
}


//	设置特殊默认值
int DbCfgFun::SetDefaultData_Special(int AppType, int TableId, char *pRecordBuff, unsigned short IndexNo, char *pDevName)
{
	if (AppType <=0 || TableId <=0 || !pRecordBuff)
	{
		return -1;
	}

	switch (AppType)
	{
	case SYSTEM_TYPE_NSA_YD:
		{
			switch (TableId)
			{
			case TABLE_RTASK_INFO:
				{
					sprintf(((rTask_Info*)pRecordBuff)->Name, "任务");
				}
				break;
			case TABLE_RDEVS_INFO:
				{
				}
				break;
			case TABLE_RDEVS_YX:
				{
					if (pDevName)
					{
						QString YxName;
						YxName = QString::fromLocal8Bit(pDevName)+QString("Yx%1").arg(IndexNo+1);

						if (YxName.length()>STRING_NORMAL_LEN)
						{
							YxName = YxName.left(STRING_NORMAL_LEN-1);
						}
						sprintf(((rDevS_Yx*)pRecordBuff)->Name, "%s", YxName.toLocal8Bit().data());
					}
					((rDevS_Yx*)pRecordBuff)->IndexNo = IndexNo+1;
				}
				break;
			case TABLE_RDEVS_RYB:
				{
					if (pDevName)
					{
						QString YxName;
						YxName = QString::fromLocal8Bit(pDevName)+QString("Ryb%1").arg(IndexNo+1);

						if (YxName.length()>STRING_NORMAL_LEN)
						{
							YxName = YxName.left(STRING_NORMAL_LEN-1);
						}
						sprintf(((rDevS_Ryb*)pRecordBuff)->Name, "%s", YxName.toLocal8Bit().data());
					}
					((rDevS_Ryb*)pRecordBuff)->IndexNo = IndexNo+1;
				}
				break;
			case TABLE_RDEVS_YC:
				{
					if (pDevName)
					{
						QString YcName;
						YcName = QString::fromLocal8Bit(pDevName)+QString("Yc%1").arg(IndexNo+1);

						if (YcName.length()>STRING_NORMAL_LEN)
						{
							YcName = YcName.left(STRING_NORMAL_LEN-1);
						}
						sprintf(((rDevS_Yc*)pRecordBuff)->Name, "%s", YcName.toLocal8Bit().data());
					}
					((rDevS_Yc*)pRecordBuff)->IndexNo = IndexNo+1;
					/*if (((rDevS_Yc*)pRecordBuff)->ycGene == 0)
					{
						((rDevS_Yc*)pRecordBuff)->ycGene = 1;
					}*/
				}
				break;
			case TABLE_RDEVS_BHYC:
				{
					if (pDevName)
					{
						QString YcName;
						YcName = QString::fromLocal8Bit(pDevName)+QString("BhYc%1").arg(IndexNo+1);

						if (YcName.length()>STRING_NORMAL_LEN)
						{
							YcName = YcName.left(STRING_NORMAL_LEN-1);
						}
						sprintf(((rDevS_BhYc*)pRecordBuff)->Name, "%s", YcName.toLocal8Bit().data());
					}
					((rDevS_BhYc*)pRecordBuff)->IndexNo = IndexNo+1;
				}
				break;
			case TABLE_RDEVS_YK:
				{
					if (pDevName)
					{
						QString YkName;
						YkName = QString::fromLocal8Bit(pDevName)+QString("Yk%1").arg(IndexNo+1);

						if (YkName.length()>STRING_NORMAL_LEN)
						{
							YkName = YkName.left(STRING_NORMAL_LEN-1);
						}
						sprintf(((rDevS_Yk*)pRecordBuff)->Name, "%s", YkName.toLocal8Bit().data());
					}
					((rDevS_Yk*)pRecordBuff)->IndexNo = IndexNo+1;
				}
				break;
			case TABLE_RDEVS_YM:
				{
					if (pDevName)
					{
						QString YmName;
						YmName = QString::fromLocal8Bit(pDevName)+QString("Ym%1").arg(IndexNo+1);

						if (YmName.length()>STRING_NORMAL_LEN)
						{
							YmName = YmName.left(STRING_NORMAL_LEN-1);
						}
						sprintf(((rDevS_Ym*)pRecordBuff)->Name, "%s", YmName.toLocal8Bit().data());
					}
					((rDevS_Ym*)pRecordBuff)->IndexNo = IndexNo+1;
				}
				break;
			case TABLE_RDEVMODEL_YX:
				{
			
					QString YxName;
					YxName = QString("Yx%1").arg(IndexNo+1);

					if (YxName.length()>STRING_NORMAL_LEN)
					{
						YxName = YxName.left(STRING_NORMAL_LEN-1);
					}
					sprintf(((rDevModel_Yx*)pRecordBuff)->Name, "%s", YxName.toLocal8Bit().data());
					((rDevModel_Yx*)pRecordBuff)->IndexNo = IndexNo+1;
				}
				break;
			default:
				break;
			}
		}
		break;
	case SYSTEM_TYPE_NSA_CD:
	default :
		break;
	}

	return 1;
}

//组织SQL Insert语句的values()部分语句
int DbCfgFun::GetInsertValueSQL(char ValueBuf[], char Recorbuff[], int Buff_len, cColumn_Info *pColumnInfo, int *pMemPos, int ColumNum, int Type)
{
	if (!ValueBuf || !Recorbuff || Buff_len<=0 || !pColumnInfo || !pMemPos || ColumNum<=0)
		return -1;

	QString qStr;
	qStr.clear();
	QString valueStr;
	for(int i=0; i<ColumNum; i++)
	{
		if (pMemPos[i]+pColumnInfo[i].clmDataLength <= Buff_len)
		{
			valueStr.clear();	
			switch(pColumnInfo[i].clmDataType) 
			{
			case DATA_TYPE_BYTE:
				{
					char charVal = 0;
					memcpy(&charVal, Recorbuff + pMemPos[i], sizeof(char));
					valueStr.setNum(charVal);
				}
				break;
			case DATA_TYPE_UCHAR:
			case DATA_TYPE_UBYTE:
				{
					unsigned char ucharVal = 0;
					memcpy(&ucharVal, Recorbuff + pMemPos[i], sizeof(unsigned char));
					valueStr.setNum(ucharVal);
				}
				break;
			case DATA_TYPE_SHORT:
				{
					short ShortVal = 0;
					memcpy(&ShortVal, Recorbuff + pMemPos[i], sizeof(short));
					valueStr.setNum((short)ShortVal);
				}
				break;
			case DATA_TYPE_USHORT:
				{
					unsigned short uShortVal = 0;
					memcpy(&uShortVal, Recorbuff + pMemPos[i], sizeof(unsigned short));
					valueStr.setNum((ushort)uShortVal);
				}
				break;
			case DATA_TYPE_INT:
				{
					int intVal = 0;
					memcpy(&intVal, Recorbuff + pMemPos[i], sizeof(int));
					valueStr.setNum((int)intVal);
				}
				break;
			case DATA_TYPE_UINT:
				{
					unsigned int uintVal = 0;
					memcpy(&uintVal, Recorbuff + pMemPos[i], sizeof(unsigned int));
					valueStr.setNum((unsigned int)uintVal);
				}
				break;
			case DATA_TYPE_FLOAT:
				{
					float floatVal =0.0;
					memcpy(&floatVal, Recorbuff + pMemPos[i], sizeof(float));
					valueStr.setNum((float)floatVal);
				}
				break;
			case DATA_TYPE_DOUBLE:
				{
					double doubleVal = 0.0;
					memcpy(&doubleVal, Recorbuff + pMemPos[i], sizeof(double));
					valueStr.setNum((double)doubleVal);
				}
				break;
			case DATA_TYPE_LONGLONG:
				{
					qint64 LonglongVal = 0;
					memcpy(&LonglongVal, Recorbuff + pMemPos[i], sizeof(qint64));
					valueStr.setNum((qint64)LonglongVal);
				}
				break;
			case DATA_TYPE_CHAR:
			case DATA_TYPE_VARCHAR:
				{
					char *pChar;
					pChar = new char[pColumnInfo[i].clmDataLength+1];
					memset(pChar, 0, pColumnInfo[i].clmDataLength+1);
					memcpy(pChar, Recorbuff + pMemPos[i], pColumnInfo[i].clmDataLength);
					pChar[pColumnInfo[i].clmDataLength] = 0;
					valueStr = QString("'%1'").arg(QString::fromLocal8Bit(pChar));
					if(pChar)
					{
						delete [](char*)pChar;
						pChar = NULL;
					}
				}
				break;
			case DATA_TYPE_VARBINARY:
			default:
				break;
			}

			if(OPER_ROW_INSERT == Type)
			{
				qStr += valueStr;
				if(i<(ColumNum-1))
				{
					qStr += QString::fromLocal8Bit(" , ");			
				}
			}
		}
	}
	if (qStr.length() >= (SQL_MAX_LEN-100) )
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("Insert values语句太长!"));
		return -2;
	}
	sprintf(ValueBuf, "%s", qStr.toLocal8Bit().data());
	return 1;
}

//组织SQL update语句的set部分语句
int DbCfgFun::GetUpdateColValueSQL(char FieldDatabuff[], int Buff_len, cColumn_Info *pColumnInfo, QString &SetSQL)
{
	SetSQL.clear();
	if (!FieldDatabuff || Buff_len<=0 || !pColumnInfo)
		return -1;

	QString valueStr;
	valueStr.clear();
	SetSQL = QString("%1=").arg(QString::fromLocal8Bit(pColumnInfo->ColumnEngName));
	switch(pColumnInfo->clmDataType) 
	{
	case DATA_TYPE_BYTE:
		{
			char charVal = 0;
			memcpy(&charVal, FieldDatabuff, sizeof(char));
			valueStr.setNum(charVal);
		}
		break;
	case DATA_TYPE_UCHAR:
	case DATA_TYPE_UBYTE:
		{
			unsigned char ucharVal = 0;
			memcpy(&ucharVal, FieldDatabuff, sizeof(unsigned char));
			valueStr.setNum(ucharVal);
		}
		break;
	case DATA_TYPE_SHORT:
		{
			short ShortVal = 0;
			memcpy(&ShortVal, FieldDatabuff, sizeof(short));
			valueStr.setNum((short)ShortVal);
		}
		break;
	case DATA_TYPE_USHORT:
		{
			unsigned short uShortVal = 0;
			memcpy(&uShortVal, FieldDatabuff, sizeof(unsigned short));
			valueStr.setNum((unsigned short)uShortVal);
		}
		break;
	case DATA_TYPE_INT:
		{
			int intVal = 0;
			memcpy(&intVal, FieldDatabuff, sizeof(int));
			valueStr.setNum((int)intVal);
		}
		break;
	case DATA_TYPE_UINT:
		{
			unsigned int uintVal = 0;
			memcpy(&uintVal, FieldDatabuff, sizeof(unsigned int));
			valueStr.setNum((unsigned int)uintVal);
		}
		break;
	case DATA_TYPE_FLOAT:
		{
			float floatVal =0.0;
			memcpy(&floatVal, FieldDatabuff, sizeof(float));
			valueStr.setNum((float)floatVal);
		}
		break;
	case DATA_TYPE_DOUBLE:
		{
			double doubleVal = 0.0;
			memcpy(&doubleVal, FieldDatabuff, sizeof(double));
			valueStr.setNum((double)doubleVal);
		}
		break;
	case DATA_TYPE_LONGLONG:
		{
			qint64 LonglongVal = 0.0;
			memcpy(&LonglongVal, FieldDatabuff, sizeof(qint64));
			valueStr.setNum((qint64)LonglongVal);
		}
		break;
	case DATA_TYPE_CHAR:
	case DATA_TYPE_VARCHAR:
		{
			char *pChar;
			pChar = new char[pColumnInfo->clmDataLength+1];
			memset(pChar, 0, pColumnInfo->clmDataLength+1);
			memcpy(pChar, FieldDatabuff, pColumnInfo->clmDataLength);
			pChar[pColumnInfo->clmDataLength] = 0;
			valueStr = QString("'%1'").arg(QString::fromLocal8Bit(pChar));
			if(pChar)
			{
				delete [](char*)pChar;
				pChar = NULL;
			}
		}
		break;
	case DATA_TYPE_VARBINARY:
	default:
		break;
	}
	SetSQL += valueStr;

	return 1;
}


// 组织SQL select语句的order by部分语句
int DbCfgFun::GetOrderBySQL_AppTypeTableId(unsigned int AppType, unsigned int TableId, QString &OrderSQL)
{
	if (AppType<=0 || TableId<=0)
	{
		return -1;
	}
	OrderSQL.clear();
	QString		str;
	char		sql[SQL_MAX_LEN];
	char		 *buff = NULL;
	int		 buff_len = 0;
	int		 Result = 0;
	int		 i = 0;
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql, "select * from ccolumn_info where TableId=%d and AppType=%d and clmKeyIndex >0 order by clmKeyIndex", TableId, AppType);
		
	Result = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_CD,TABLE_CCOLUMN_INFO, sql, buff, &buff_len);
	if (Result>0 && buff && buff_len>0)
	{
		OrderSQL = QString("Order by ");
		for (i=0; i<Result; i++)
		{
			str = QString("%1").arg(QString::fromLocal8Bit(((cColumn_Info*)buff)[i].ColumnEngName));
			OrderSQL += str;
			if (i<Result-1)
			{
				str = QString(" , ");
				OrderSQL += str;
			}
		}
	}

	if (buff)
	{
		delete [](char*)buff; 
		buff = NULL;
	}

	return Result;
}

// 组织SQL Insert语句并插入记录
int DbCfgFun::InsertRecordIntoDB(int AppType, int TableId, char *pDataBuff, int Buff_len, cColumn_Info *pColumnInfo, int *pMemPos, int ColumnNum)
{
	if (AppType<=0 || TableId<=0 || !pDataBuff || Buff_len<=0 || !pColumnInfo || !pMemPos || ColumnNum<=0 )
	{
		return -1;
	}
	int	Result = 0;
	char	*tableName = NULL;
	int	Name_len = 0;
	char	sql[SQL_MAX_LEN];
	char	ValSql[SQL_MAX_LEN];
	memset(sql, 0, SQL_MAX_LEN);
	memset(ValSql, 0, SQL_MAX_LEN);
	Result = GetInsertValueSQL(ValSql, pDataBuff, Buff_len, pColumnInfo, pMemPos, ColumnNum, OPER_ROW_INSERT);
	if (Result>0)
	{
		GetTableEngNamebyAppType_TableId(AppType, TableId, tableName, &Name_len);
		if (tableName)
		{
			sprintf(sql, "insert into %s values(%s)", tableName, ValSql);
			delete [](char*)tableName;
			tableName = NULL;
		}else
		{
			QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("获取表名失败，表ID[%1], 应用类型[%1]").arg(TableId).arg(AppType));
			return -4;
		}
		Result = g_pSqLiteDB.DB_Modify(sql);
		return Result;
	}else
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("insert SQL语句拼写有误，请检查，表ID[%1], 应用类型[%1]").arg(TableId).arg(AppType));
		return -5;
	}

	return 0;
}

// 组织SQL Insert语句并插入记录
int DbCfgFun::InsertRecordIntoDB(int AppType, int TableId, char *pDataBuff, int Buff_len, cColumn_Info *pColumnInfo, int *pMemPos, int ColumnNum, char *pTableName)
{
	if (AppType<=0 || TableId<=0 || !pDataBuff || Buff_len<=0 || !pColumnInfo || !pMemPos || ColumnNum<=0 || !pTableName)
	{
		return -1;
	}

	int	Result = 0;
	char	sql[SQL_MAX_LEN];
	char	ValSql[SQL_MAX_LEN];
	memset(sql, 0, SQL_MAX_LEN);
	memset(ValSql, 0, SQL_MAX_LEN);
	Result = GetInsertValueSQL(ValSql, pDataBuff, Buff_len, pColumnInfo, pMemPos, ColumnNum, OPER_ROW_INSERT);
	if (Result>0)
	{
		sprintf(sql, "replace into %s values(%s)", pTableName, ValSql);
		Result = g_pSqLiteDB.DB_Modify(sql);
		if (Result<0)
		{
			Result=-6;
		}
		return Result;
	}else
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("insert SQL语句拼写有误，请检查，表ID[%1], 应用类型[%1]").arg(TableId).arg(AppType));
		return -5;
	}

	return 0;
}


//组织SQL delete语句并删除记录
int	DbCfgFun::DeleteAllRecord(int AppType, int TableId)
{
	if (AppType<=0 || TableId<=0 )
	{
		return -1;
	}
	char *tableName = NULL;
	int   Name_len = 0;
	char sql[SQL_MAX_LEN];			
	memset(sql, 0, SQL_MAX_LEN);
	GetTableEngNamebyAppType_TableId(AppType, TableId, tableName, &Name_len);
	if (tableName)
	{
		sprintf(sql, "delete from %s ", tableName);
		delete [](char*)tableName;
		tableName = NULL;
	}else
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("获取表名失败，表ID[%1], 应用类型[%1]").arg(TableId).arg(AppType));
		return -2;
	}
	return g_pSqLiteDB.DB_Modify(sql);

}

int	DbCfgFun::DeleteAllRecord(int AppType, int TableId, char *pTableName)
{
	if (AppType<=0 || TableId<=0 || !pTableName)
	{
		return -1;
	}
	char sql[SQL_MAX_LEN];			
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql, "delete from %s", pTableName);
	return g_pSqLiteDB.DB_Modify(sql);
}

//	组织SQL Insert语句并插入记录
int DbCfgFun::DeleteRecordByID(int AppType, int TableId, unsigned int RecordId)
{
	if (AppType<=0 || TableId<=0 || RecordId<=0 )
	{
		return -1;
	}
	char *tableName = NULL;
	int   Name_len = 0;
	char sql[SQL_MAX_LEN];			
	memset(sql, 0, SQL_MAX_LEN);
	GetTableEngNamebyAppType_TableId(AppType, TableId, tableName, &Name_len);
	if (tableName)
	{
		sprintf(sql, "delete from %s where ID=%d", tableName, RecordId);
		delete [](char*)tableName;
		tableName = NULL;
	}else
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("获取表名失败，表ID[%1], 应用类型[%1]").arg(TableId).arg(AppType));
		return -2;
	}
	return g_pSqLiteDB.DB_Modify(sql);
	
	return 0;
}

//	组织SQL Insert语句并插入记录
int DbCfgFun::DeleteRecordByID(int AppType, int TableId, unsigned int RecordId, char *pTableName)
{
	if (AppType<=0 || TableId<=0 || RecordId<=0 || !pTableName)
	{
		return -1;
	}
	char sql[SQL_MAX_LEN];			
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql, "delete from %s where ID=%d", pTableName, RecordId);
	return g_pSqLiteDB.DB_Modify(sql);
}

//	组织SQL delete语句并删除记录
int DbCfgFun::DeleteRecordByParentId(int AppType, int TableId, unsigned int ParentId)
{
	if (AppType<=0 || TableId<=0 || ParentId<=0 )
	{
		return -1;
	}
	char *tableName = NULL;
	int   Name_len = 0;
	char sql[SQL_MAX_LEN];			
	memset(sql, 0, SQL_MAX_LEN);
	GetTableEngNamebyAppType_TableId(AppType, TableId, tableName, &Name_len);
	if (tableName)
	{
		sprintf(sql, "delete from %s where ParentId=%d", tableName, ParentId);
		delete [](char*)tableName;
		tableName = NULL;
	}else
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("获取表名失败，表ID[%1], 应用类型[%1]").arg(TableId).arg(AppType));
		return -2;
	}
	return g_pSqLiteDB.DB_Modify(sql);

	return 0;
}

//	组织SQL delete语句并删除记录
int DbCfgFun::DeleteRecordByParentId(int AppType, int TableId, unsigned int ParentId, char *pTableName)
{
	if (AppType<=0 || TableId<=0 || ParentId<=0 || !pTableName)
	{
		return -1;
	}
	char sql[SQL_MAX_LEN];			
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql, "delete from %s where ParentId=%d", pTableName, ParentId);
	return g_pSqLiteDB.DB_Modify(sql);
}

// 修改记录名通过ID
int DbCfgFun::UpdataRecordNamebyID(int AppType, int TableId, unsigned int RecordId, char *pTableName, char *RecordName)
{
	if (RecordId<=0 || !RecordName || !pTableName || AppType<=0 || TableId<=0)
	{
		return -1;
	}
	if (QString::fromLocal8Bit(pTableName).length()<=0 ||
		QString::fromLocal8Bit(RecordName).length()<=0 )
	{
		return -2;
	}

	char	 sql[SQL_MAX_LEN];
	memset(sql, 0, SQL_MAX_LEN);

	sprintf(sql, "update %s set Name='%s' where ID=%d ",pTableName, RecordName,  RecordId);

	return g_pSqLiteDB.DB_Modify(sql);
}

// 组织SQL update语句并修改记录字段
int DbCfgFun::UpDateOneColumnData(cColumn_Info *pColInfo, unsigned int RecordId, char *pDataBuff, int Bufflen, char *pTableName)
{
	if (RecordId<=0 || !pColInfo || !pDataBuff || !pTableName || Bufflen<=0)
	{
		return -1;
	}

	char	 sql[SQL_MAX_LEN];
	QString	 SetValStr = 0;
	int	 Result = 0;

	memset(sql, 0, SQL_MAX_LEN);

	Result = GetUpdateColValueSQL(pDataBuff, Bufflen, pColInfo, SetValStr);
	if (Result>0)
	{
		sprintf(sql, "update %s set %s where ID=%d ", pTableName, SetValStr.toLocal8Bit().data(), RecordId);
	}else
	{
		return -2;
	}
	
	return g_pSqLiteDB.DB_Modify(sql);
}

// 组织SQL update语句并修改记录字段
int DbCfgFun::UpDateOneColumnData(cColumn_Info *pColInfo, unsigned int RecordId, char *pDataBuff, int Bufflen)
{
	if (RecordId<=0 || !pColInfo || !pDataBuff || Bufflen<=0)
	{
		return -1;
	}

	char	 sql[SQL_MAX_LEN];
	QString	 SetValStr = 0;
	int	 Result = 0;
	char *tableName = NULL;
	int   Name_len = 0;

	memset(sql, 0, SQL_MAX_LEN);

	Result = GetUpdateColValueSQL(pDataBuff, Bufflen, pColInfo, SetValStr);
	if (Result <= 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("获取SQL Update语句失败！"));
		return -2;
	}

	GetTableEngNamebyAppType_TableId(pColInfo->AppType, pColInfo->TableId, tableName, &Name_len);
	if (tableName)
	{
		sprintf(sql, "update %s set %s where ID=%d ", tableName, SetValStr.toLocal8Bit().data(), RecordId);
		delete [](char*)tableName;
		tableName = NULL;
	}else
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("获取表名失败，表ID[%1], 应用类型[%1]").arg(pColInfo->TableId).arg(pColInfo->AppType));
		return -3;
	}

	return g_pSqLiteDB.DB_Modify(sql);
}

// 组织SQL update语句并修改记录字段
int DbCfgFun::UpDateOneColumnData(int AppType, int TableId, QString sTableName, QString sClmEngName, unsigned int Data_uint, unsigned int RecordId)
{
	if (AppType <=0 || TableId <=0 || sTableName.length()<=0 || sClmEngName.length()<=0 || RecordId<=0)
	{
		return -1;
	}

	char	 sql[SQL_MAX_LEN];
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql, "update %s set %s=%d where ID=%d ", sTableName.toLocal8Bit().data(), sClmEngName.toLocal8Bit().data(), Data_uint, RecordId);
	
	return g_pSqLiteDB.DB_Modify(sql);
}

//	清空记录字段值为0
int DbCfgFun::ClearColumnData(int AppType, int TableId, QString sTableName, QString sClmEngName, unsigned int ColumnData)
{
	if (AppType <=0 || TableId <=0 || sTableName.length()<=0 || sClmEngName.length()<=0 || ColumnData<=0)
	{
		return -1;
	}

	char	 sql[SQL_MAX_LEN];
	int	 Result = 0;
	int	 Count = 0;
	char	 *buff = NULL;
	int	 buff_len =0;
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql, "select count(*) from %s where %s=%d", sTableName.toLocal8Bit().data(), sClmEngName.toLocal8Bit().data(), ColumnData);
	Result = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (Result > 0 && buff && buff_len>0)
	{
		memcpy(&Count, buff, sizeof(int));
		//qDebug()<<"ok Result="<<Result<<"\r\n";
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
	if (Result=0)
	{
		return 1;
	}
	if (Result<0)
	{
		//qDebug()<<"fail Result="<<Result<<"\r\n";
		return Result;
	}
	if (Count>0)
	{
		memset(sql, 0, SQL_MAX_LEN);
		sprintf(sql, "update %s set %s=%d where %s=%d ", sTableName.toLocal8Bit().data(), sClmEngName.toLocal8Bit().data(), 0, sClmEngName.toLocal8Bit().data(), ColumnData);
		return g_pSqLiteDB.DB_Modify(sql);
	}else
	{
		return 0;
	}
}

//	删除记录通过字段值
int DbCfgFun::DeleteRecordbyColData(int AppType, int TableId, QString sTableName, QString sClmEngName, unsigned int ColumnData)
{
	if (AppType <=0 || TableId <=0 || sTableName.length()<=0 || sClmEngName.length()<=0 || ColumnData<=0)
	{
		return -1;
	}

	char	 sql[SQL_MAX_LEN];
	int	 Result = 0;
	int	 Count = 0;
	char	 *buff = NULL;
	int	 buff_len =0;
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql, "select count(*) from %s where %s=%d ", sTableName.toLocal8Bit().data(), sClmEngName.toLocal8Bit().data(), ColumnData);
	Result = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (Result > 0 && buff && buff_len>0)
	{
		memcpy(&Count, buff, sizeof(int));
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
	if (Result<0)
	{
		return Result;
	}
	if (Count>0)
	{
		memset(sql, 0, SQL_MAX_LEN);
		sprintf(sql, "delete from %s where %s=%d ", sTableName.toLocal8Bit().data(), sClmEngName.toLocal8Bit().data(), ColumnData);
		return g_pSqLiteDB.DB_Modify(sql);
	}else
	{
		return 0;
	}
}

//	清空遥信记录中的的配置主遥控或副遥控
int DbCfgFun::ClearYx_RelateYkOrRelateSubYk(unsigned int YxRecordId, unsigned int RelateYkId)
{
	if (YxRecordId <= 0 || RelateYkId <=0 )
	{
		return -1;
	}

	QString sColumnName;
	QString sTableName;
	sTableName = QString::fromLocal8Bit("rDevS_Yx");
	sColumnName.clear();
	char *pbuff = NULL;
	int  buff_len = 0;
	GetRecordBuffbyAppType_TableId_RecordId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, YxRecordId, pbuff, &buff_len);
	if (pbuff && buff_len>0)
	{
		if ( ((rDevS_Yx*)pbuff)->RelYkId%PARENT_OFFSET == RelateYkId)
		{
			sColumnName = QString::fromLocal8Bit("RelYkID");
		}
	}
	if (pbuff)
	{
		delete [](char*)pbuff;
		pbuff = NULL;
	}
	if (!sColumnName.isEmpty())
	{
		return UpDateOneColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, sTableName, sColumnName, 0, YxRecordId);
	}
	return 0;
}

//	判断当前设备地址在通道中是否重复
bool DbCfgFun::IsCurDevAddrRepeatInRout(int DevAddr, unsigned char DevType, unsigned int ParentId)
{
	if (DevAddr<=0 || ParentId<=0)
	{
		return false;
	}

	char	Sql[SQL_MAX_LEN];
	char	*Buff = NULL;
	int	buff_len = 0;
	int	Result = 0;
	int	Count = 0;

	sprintf(Sql, "select count(*) from rDevS_Info where LinkAddr=%d and DevType=%d and ParentId=%d", DevAddr, DevType, ParentId);
	Result = g_pSqLiteDB.DB_SelectUInt(Sql, Buff, &buff_len);
	if (Result > 0 && Buff && buff_len>0)
	{
		memcpy(&Count, Buff, sizeof(int));
	}

	if (Buff)
	{
		delete []Buff;
		Buff = NULL;
	}
	
	if (Count>0)
	{
		return true;
	}else
	{
		return false;
	}
	return false;
}

//检查记录是否安全
bool DbCfgFun::CheckDataIsSaft(int AppType, int TableId, char *pRecordBuff, int OperType)
{
	if (!pRecordBuff || (SYSTEM_TYPE_NSA_YD != AppType/* && SYSTEM_TYPE_NSA_GD != AppType*/) || TableId <= 0)
	{
		return false;
	}

	switch (TableId)
	{
	/*case TABLE_RNODE_INFO:
		{
			
			int Ip_len_a= QString::fromLocal8Bit(((rNode_Info*)pRecordBuff)->ndIP_A).length();
			int Ip_len_b= QString::fromLocal8Bit(((rNode_Info*)pRecordBuff)->ndIP_B).length();
			if ((NET_TYPE_SINGLE == ((rNode_Info*)pRecordBuff)->ndNetMode ||
				NET_TYPE_DOUBLE == ((rNode_Info*)pRecordBuff)->ndNetMode)	 && 
				Ip_len_a<=0)
			{
				QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("节点[%1]IP_A未设置!").arg(QString::fromLocal8Bit(((rNode_Info*)pRecordBuff)->ByName)));
				return false;
			}
			if (NET_TYPE_DOUBLE == ((rNode_Info*)pRecordBuff)->ndNetMode && 
				Ip_len_b<=0)
			{
				QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("节点[%1]IP_B未设置!").arg(QString::fromLocal8Bit(((rNode_Info*)pRecordBuff)->ByName)));
				return false;
			}
			if (Ip_len_a>0 && !CheckIpIsSafe(((rNode_Info*)pRecordBuff)->ndIP_A, IP_STR_LEN)) 
			{
				QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("节点[%1]IP_A非法!").arg(QString::fromLocal8Bit(((rNode_Info*)pRecordBuff)->ByName)));
				return false;
			}
			if (Ip_len_b>0 && !CheckIpIsSafe(((rNode_Info*)pRecordBuff)->ndIP_B, IP_STR_LEN)) 
			{
				QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("节点[%1]IP_B非法!").arg(QString::fromLocal8Bit(((rNode_Info*)pRecordBuff)->ByName)));
				return false;
			}
			if (OPER_ROW_UPDATE == OperType)
			{
				XBYTE ndKind = 0;
				ndKind =((rNode_Info*)pRecordBuff)->ndKind;
				if( (ndKind & NODE_KIND_SERVER) == 0 && CurNodeIsHasChild(((rNode_Info*)pRecordBuff)->ID, NODE_KIND_SERVER))
				{
					QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点[%1]已经配置了服务器，不允许修改配置！").arg(QString::fromLocal8Bit(((rNode_Info*)pRecordBuff)->ByName)));
					return false;
				}
				if ( (ndKind & NODE_KIND_WORKSTATION)== 0 && CurNodeIsHasChild(((rNode_Info*)pRecordBuff)->ID, NODE_KIND_WORKSTATION) )
				{
					QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点[%1]已经配置了工作站，不允许修改配置！").arg(QString::fromLocal8Bit(((rNode_Info*)pRecordBuff)->ByName)));
					return false;
				}
				if ( (ndKind&NODE_KIND_FRONT_SERVER)== 0 && CurNodeIsHasChild(((rNode_Info*)pRecordBuff)->ID, NODE_KIND_FRONT_SERVER) )
				{
					QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点[%1]已经配置了前置机，不允许修改配置！").arg(QString::fromLocal8Bit(((rNode_Info*)pRecordBuff)->ByName)));
					return false;
				}
			}
		}
		break;
	case TABLE_RSERVER_UNIT:
		{

		}
		break;
	case TABLE_RWORKSTATION_UNIT:
		{

		}
		break;
	case TABLE_RFRONTSERVER_UNIT:
		{

		}
		break;
	case TABLE_RFRONTROUT_INFO:
		{
			if (SYS_STATE_INVALID == ((rFrontRout_Info*)pRecordBuff)->frtIsTransmit &&
				CurRecordIsHasChild(SYSTEM_TYPE_NSA_RD, TABLE_RFRONTROUT_INFO,((rFrontRout_Info*)pRecordBuff)->ID, TABLE_RTRANSMIT_PARAM_INFO))
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前通道[%1]配置有转发信息，修改无效！").arg(QString::fromLocal8Bit(((rFrontRout_Info*)pRecordBuff)->Name)));
				return false;
			}
			if (SYS_STATE_INVALID == ((rFrontRout_Info*)pRecordBuff)->frtIsCollect &&
				CurRecordIsHasChild(SYSTEM_TYPE_NSA_RD, TABLE_RFRONTROUT_INFO,((rFrontRout_Info*)pRecordBuff)->ID, TABLE_RDEVICE_INFO))
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前通道[%1]配置有设备，修改无效！").arg(QString::fromLocal8Bit(((rFrontRout_Info*)pRecordBuff)->Name)));
				return false;
			}
		}
		break;
	case TABLE_RDEVICE_INFO:
		{
			char TableName[TABLE_ENG_NAME_LEN];
			memset(TableName, 0, TABLE_ENG_NAME_LEN);
			sprintf(TableName, "rdevice_info");
			if (((rDevice_Info*)pRecordBuff)->ParentId<=0)
			{
				return false;
			}

			if (!IsByNameSafe(TableName, ((rDevice_Info*)pRecordBuff)->ByName,((rDevice_Info*)pRecordBuff)->ID))
			{
				return false;
			}
			return true;
		}
		break;
	case TABLE_RYXDEFINE_INFO:
		{

		}
		break;
	case TABLE_RYCDEFINE_INFO:
		{

		}
		break;
	case TABLE_RYKDEFINE_INFO:
		{

		}
		break;
	case TABLE_RYMDEFINE_INFO:
		{

		}
		break;
	case TABLE_RYTDEFINE_INFO:
		{

		}
		break;*/
	default:
		break;
	}

	return true;
}

//	获取四遥的设备ID
int DbCfgFun::GetParentIdForSIYAO(int TableId, unsigned int RecordId, unsigned int &ParentId)
{
	ParentId = 0;
	char		sql[SQL_MAX_LEN];
	char		*buff = NULL;
	int		buff_len = 0;
	int		Ret = 0;
	char		TableName[TABLE_ENG_NAME_LEN];
	int		AppType = SYSTEM_TYPE_NSA_YD;
	memset(sql, 0, SQL_MAX_LEN);
	memset(TableName, 0, TABLE_ENG_NAME_LEN);
	switch (TableId)
	{
	case TABLE_RDEVS_YX:
		{
			sprintf(TableName, "rDevS_Yx");
		}
		break;
	case TABLE_RDEVS_RYB:
		{
			sprintf(TableName, "rRybS_Info");
		}
		break;
	case TABLE_RDEVS_YC:
		{
			sprintf(TableName, "rYcs_Info");
		}
		break;
	case TABLE_RDEVS_BHYC:
		{
			sprintf(TableName, "rBhYcs_Info");
		}
		break;
	case TABLE_RDEVS_YK:
		{
			sprintf(TableName, "rYks_Info");
		}
		break;
	case TABLE_RDEVS_YM:
		{
			sprintf(TableName, "rDevS_Ym");
		}
		break;
	default:
		return -1;
		break;
	}

	sprintf(sql, "select ParentId from %s where ID=%d", TableName, RecordId);
	Ret = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (Ret>0 && buff && buff_len>0)
	{
		memcpy(&ParentId, buff, sizeof(unsigned int));
	}

	if (buff)
	{
		delete [](char *)buff;
		buff = NULL;
	}
	return Ret;
}

//	获取四遥的名称
int DbCfgFun::GetSIYAONameFromParentIdAndIndexNo(int TableId, unsigned int ParentId, unsigned short IndexNo, QString &NameStr)
{
	NameStr.clear();
	if (TableId<=0 || ParentId <=0 || IndexNo<=0)
	{
		return -1;
	}

	char		sql[SQL_MAX_LEN];
	char		*buff = NULL;
	int		buff_len = 0;
	int		Ret = 0;
	char		TableName[TABLE_ENG_NAME_LEN];
	int		AppType = SYSTEM_TYPE_NSA_YD;
	memset(sql, 0, SQL_MAX_LEN);
	memset(TableName, 0, TABLE_ENG_NAME_LEN);
	switch (TableId)
	{
	case TABLE_RDEVS_YX:
		{
			sprintf(TableName, "rDevS_Yx");
		}
		break;
	case TABLE_RDEVS_RYB:
		{
			sprintf(TableName, "rRybS_Info");
		}
		break;
	case TABLE_RDEVS_BHYC:
		{
			sprintf(TableName, "rDevS_BhYc");
		}
		break;
	case TABLE_RDEVS_YC:
		{
			sprintf(TableName, "rYcs_Info");
		}
		break;
	case TABLE_RDEVS_YK:
		{
			sprintf(TableName, "rYks_Info");
		}
		break;
	case TABLE_RDEVS_YM:
		{
			sprintf(TableName, "rDevS_Ym");
		}
		break;
	default:
		return -1;
		break;
	}
	
	sprintf(sql, "select Name from %s where ParentId=%d and IndexNo=%d", TableName, ParentId, IndexNo);
	Ret = g_pSqLiteDB.DB_SelectString(NAME_SIZE, sql, buff, &buff_len);
	if (Ret>0 && buff && buff_len>0)
	{
		NameStr = QString::fromLocal8Bit(buff);
	}

	if (buff)
	{
		delete [](char *)buff;
		buff = NULL;
	}
	return Ret;
}	

//调整四遥点中设备的名称
void DbCfgFun::AdjustDevNameForSiYao(char *PreName, char *AfterName, int DeviceId)
{
	if (!PreName || !AfterName || DeviceId <= 0)
		return ;

	int	Ret = 0;
	char	*buff = NULL;
	int	buff_len = 0;
	char	NameBuff[STRING_NORMAL_LEN];
	char	Tempbuff[STRING_NORMAL_LEN];
	int	NameLen = 0;
	int	YxNum = 0;
	int	YcNum = 0;
	int	YmNum = 0;
	int	YkNum = 0;
	int BhycNum = 0;
	int rybNum=0;
	int	RlySOENum = 0;
	int	FaultNum = 0;
	QString	NameStr;
	QString	PreDevNameStr;
	QString	afterDevNameStr;
	PreDevNameStr = QString::fromLocal8Bit(PreName);
	afterDevNameStr = QString::fromLocal8Bit(AfterName);
	char TableName[STRING_NORMAL_LEN];

	memset(TableName, 0, STRING_NORMAL_LEN);
	sprintf(TableName, "rDevS_Yx");
	//YX
	YxNum = GetRecordBuffbyTableName_ParentId(TableName,
											  DeviceId+TABLE_RDEVS_INFO*PARENT_OFFSET, 
											  buff, 
											  &buff_len);
	if (YxNum >0 && buff && buff_len>0)
	{
		for (int YxNo = 0; YxNo < YxNum; YxNo++)
		{
			memset(NameBuff, 0, STRING_NORMAL_LEN);
			memset(Tempbuff, 0, STRING_NORMAL_LEN);
			memcpy(Tempbuff, ((rDevS_Yx *)buff)[YxNo].Name, STRING_NORMAL_LEN);
			NameStr = QString::fromLocal8Bit(Tempbuff);
			if (NameStr.contains(PreDevNameStr, Qt::CaseSensitive))
			{	
				NameStr.replace(PreDevNameStr, afterDevNameStr, Qt::CaseSensitive);
			}else
			{	
				NameStr.insert(0, afterDevNameStr);
			}
			NameLen = NameStr.toLocal8Bit().length();
			if (NameLen>=STRING_NORMAL_LEN)
			{
				NameLen = STRING_NORMAL_LEN-1;
			}
			memcpy(NameBuff, NameStr.toLocal8Bit().data(), NameLen);
			Ret = UpdataRecordNamebyID(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, ((rDevS_Yx *)buff)[YxNo].ID, TableName, NameBuff);
			if (Ret<0)
			{
				break;
			}
		}
	}

	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

//ryb
	memset(TableName, 0, STRING_NORMAL_LEN);
	sprintf(TableName, "rDevS_Ryb");
	
	rybNum = GetRecordBuffbyTableName_ParentId(TableName,
		DeviceId+TABLE_RDEVS_INFO*PARENT_OFFSET, 
		buff, 
		&buff_len);
	if (rybNum >0 && buff && buff_len>0)
	{
		for (int rybNo = 0; rybNo < rybNum; rybNo++)
		{
			memset(NameBuff, 0, STRING_NORMAL_LEN);
			memset(Tempbuff, 0, STRING_NORMAL_LEN);
			memcpy(Tempbuff, ((rDevS_Ryb *)buff)[rybNo].Name, STRING_NORMAL_LEN);
			NameStr = QString::fromLocal8Bit(Tempbuff);
			if (NameStr.contains(PreDevNameStr, Qt::CaseSensitive))
			{	
				NameStr.replace(PreDevNameStr, afterDevNameStr, Qt::CaseSensitive);
			}else
			{	
				NameStr.insert(0, afterDevNameStr);
			}
			NameLen = NameStr.toLocal8Bit().length();
			if (NameLen>=STRING_NORMAL_LEN)
			{
				NameLen = STRING_NORMAL_LEN-1;
			}
			memcpy(NameBuff, NameStr.toLocal8Bit().data(), NameLen);
			Ret = UpdataRecordNamebyID(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_RYB, ((rDevS_Ryb *)buff)[rybNo].ID, TableName, NameBuff);
			if (Ret<0)
			{
				break;
			}
		}
	}

	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
	//YC
	memset(TableName, 0, STRING_NORMAL_LEN);
	sprintf(TableName, "rDevS_Yc");
	YcNum = GetRecordBuffbyTableName_ParentId(TableName,
											DeviceId+TABLE_RDEVS_INFO*PARENT_OFFSET, 
											buff, 
											&buff_len);
	if (buff_len >0 && buff && YcNum>0)
	{
		for (int YcNo = 0; YcNo < YcNum; YcNo++)
		{
			memset(NameBuff, 0, STRING_NORMAL_LEN);
			memset(Tempbuff, 0, STRING_NORMAL_LEN);
			memcpy(Tempbuff, ((rDevS_Yc *)buff)[YcNo].Name, STRING_NORMAL_LEN);
			NameStr = QString::fromLocal8Bit(Tempbuff);
			if (NameStr.contains(PreDevNameStr, Qt::CaseSensitive))
			{	
				NameStr.replace(PreDevNameStr, afterDevNameStr, Qt::CaseSensitive);
			}else
			{	
				NameStr.insert(0, afterDevNameStr);
			}
			NameLen = NameStr.toLocal8Bit().length();
			if (NameLen>=STRING_NORMAL_LEN)
			{
				NameLen = STRING_NORMAL_LEN-1;
			}
			memcpy(NameBuff, NameStr.toLocal8Bit().data(), NameLen);
			Ret = UpdataRecordNamebyID(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, ((rDevS_Yc *)buff)[YcNo].ID, TableName, NameBuff);
			if (Ret<0)
			{
				break;
			}
		}
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
//// BHYC
	memset(TableName, 0, STRING_NORMAL_LEN);
	sprintf(TableName, "rDevS_BhYc");
	YcNum = GetRecordBuffbyTableName_ParentId(TableName,
		DeviceId+TABLE_RDEVS_INFO*PARENT_OFFSET, 
		buff, 
		&buff_len);
	if (buff_len >0 && buff && YcNum>0)
	{
		for (int YcNo = 0; YcNo < YcNum; YcNo++)
		{
			memset(NameBuff, 0, STRING_NORMAL_LEN);
			memset(Tempbuff, 0, STRING_NORMAL_LEN);
			memcpy(Tempbuff, ((rDevS_BhYc *)buff)[YcNo].Name, STRING_NORMAL_LEN);
			NameStr = QString::fromLocal8Bit(Tempbuff);
			if (NameStr.contains(PreDevNameStr, Qt::CaseSensitive))
			{	
				NameStr.replace(PreDevNameStr, afterDevNameStr, Qt::CaseSensitive);
			}else
			{	
				NameStr.insert(0, afterDevNameStr);
			}
			NameLen = NameStr.toLocal8Bit().length();
			if (NameLen>=STRING_NORMAL_LEN)
			{
				NameLen = STRING_NORMAL_LEN-1;
			}
			memcpy(NameBuff, NameStr.toLocal8Bit().data(), NameLen);
			Ret = UpdataRecordNamebyID(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_BHYC, ((rDevS_BhYc *)buff)[YcNo].ID, TableName, NameBuff);
			if (Ret<0)
			{
				break;
			}
		}
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	//Ym
	memset(TableName, 0, STRING_NORMAL_LEN);
	sprintf(TableName, "rYmS_info");
	YmNum = GetRecordBuffbyTableName_ParentId(TableName,
											DeviceId+TABLE_RDEVS_INFO*PARENT_OFFSET, 
											buff, 
											&buff_len);
	if (buff_len >0 && buff && YmNum>0)
	{
		for (int YmNo = 0; YmNo < YmNum; YmNo++)
		{
			memset(NameBuff, 0, STRING_NORMAL_LEN);
			memset(Tempbuff, 0, STRING_NORMAL_LEN);
			memcpy(Tempbuff, ((rDevS_Ym *)buff)[YmNo].Name, STRING_NORMAL_LEN);
			NameStr = QString::fromLocal8Bit(Tempbuff);
			if (NameStr.contains(PreDevNameStr, Qt::CaseSensitive))
			{	
				NameStr.replace(PreDevNameStr, afterDevNameStr, Qt::CaseSensitive);
			}else
			{	
				NameStr.insert(0, afterDevNameStr);
			}
			NameLen = NameStr.toLocal8Bit().length();
			if (NameLen>=STRING_NORMAL_LEN)
			{
				NameLen = STRING_NORMAL_LEN-1;
			}
			memcpy(NameBuff, NameStr.toLocal8Bit().data(), NameLen);
			Ret = UpdataRecordNamebyID(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YM, ((rDevS_Ym *)buff)[YmNo].ID, TableName, NameBuff);
			if (Ret<0)
			{
				break;
			}
		}
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	//Yk
	memset(TableName, 0, STRING_NORMAL_LEN);
	sprintf(TableName, "rYkS_info");
	YkNum = GetRecordBuffbyTableName_ParentId(TableName,
												DeviceId+TABLE_RDEVS_INFO*PARENT_OFFSET, 
												buff, 
												&buff_len);
	if (buff_len >0 && buff && YkNum>0)
	{
		for (int YkNo = 0; YkNo < YkNum; YkNo++)
		{
			memset(NameBuff, 0, STRING_NORMAL_LEN);
			memset(Tempbuff, 0, STRING_NORMAL_LEN);
			memcpy(Tempbuff, ((rDevS_Yk *)buff)[YkNo].Name, STRING_NORMAL_LEN);
			NameStr = QString::fromLocal8Bit(Tempbuff);
			if (NameStr.contains(PreDevNameStr, Qt::CaseSensitive))
			{	
				NameStr.replace(PreDevNameStr, afterDevNameStr, Qt::CaseSensitive);
			}else
			{	
				NameStr.insert(0, afterDevNameStr);
			}
			NameLen = NameStr.toLocal8Bit().length();
			if (NameLen>=STRING_NORMAL_LEN)
			{
				NameLen = STRING_NORMAL_LEN-1;
			}
			memcpy(NameBuff, NameStr.toLocal8Bit().data(), NameLen);
			Ret = UpdataRecordNamebyID(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, ((rDevS_Yk *)buff)[YkNo].ID, TableName, NameBuff);
			if (Ret<0)
			{
				break;
			}
		}
	}
	if (buff)
	{
		delete []buff;
		buff = NULL;
	}
}

int DbCfgFun::GetMenuInfoByMenuName(char *MenuName, char *&buff)
{
	if (buff != NULL || !MenuName)
	{
		return -1;
	}

	char	sql[SQL_MAX_LEN];
	int	buff_len = 0;
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql,"select * from cmenu_info where menuName='%s' order by menuNo",MenuName);
	int MenuNum = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_CD,TABLE_CMENU_INFO, sql, buff, &buff_len);
	if( MenuNum <=0 )
	{
		if (buff)
		{
			free(buff);
			buff=NULL;
		}
	}
	return MenuNum;
}

int DbCfgFun::GetTaskIdAndDevIdAndIndexNoByIndexId(int m_RelateId,unsigned int IndexId,unsigned int &TaskId,unsigned int &DevId,unsigned int &IndexNo)
{
	char		*buff = NULL;
	int			buff_len = 0;
	int			Num = 0;

	unsigned int	TaskIdTemp=0;
	unsigned int	DevIdTemp=0;
	unsigned int	IndexNoTemp=0;

	rDevS_Yx	yxs_temp;
	rDevS_Yc	ycs_temp;
	rDevS_Yk	yks_temp;
	rDevS_Ym	yms_temp;
	rDevS_Ryb	rybs_temp;
	rDevS_BhYc	bhycs_temp;
	rDevS_Info	devs_temp;

	memset(&yxs_temp,0x0,sizeof(rDevS_Yx));
	memset(&ycs_temp,0x0,sizeof(rDevS_Yc));
	memset(&yks_temp,0x0,sizeof(rDevS_Yk));
	memset(&yms_temp,0x0,sizeof(rDevS_Ym));
	memset(&devs_temp,0x0,sizeof(rDevS_Info));

	memset(&rybs_temp,0x0,sizeof(rDevS_Ryb));
	memset(&bhycs_temp,0x0,sizeof(rDevS_BhYc));

	Num = GetRecordBuffbyAppType_TableId_RecordId(SYSTEM_TYPE_NSA_YD, m_RelateId, IndexId%PARENT_OFFSET, buff, &buff_len);

	if (Num==1 && buff && buff_len>0)
	{
		switch (m_RelateId)
		{
		case TABLE_RDEVS_YX:
			{
				memcpy(&yxs_temp,buff,sizeof(rDevS_Yx));
				DevIdTemp=yxs_temp.ParentId;
				IndexNoTemp=yxs_temp.IndexNo;
			}
			break;
		case TABLE_RDEVS_RYB:
			{
				memcpy(&rybs_temp,buff,sizeof(rDevS_Ryb));
				DevIdTemp=rybs_temp.ParentId;
				IndexNoTemp=rybs_temp.IndexNo;
			}
			break;
		case TABLE_RDEVS_YC:
			{
				memcpy(&ycs_temp,buff,sizeof(rDevS_Yc));
				DevIdTemp=ycs_temp.ParentId;
				IndexNoTemp=ycs_temp.IndexNo;
			}
			break;
		case TABLE_RDEVS_BHYC:
			{
				memcpy(&bhycs_temp,buff,sizeof(rDevS_BhYc));
				DevIdTemp=bhycs_temp.ParentId;
				IndexNoTemp=bhycs_temp.IndexNo;
			}
			break;
		case TABLE_RDEVS_YK:
			{
				memcpy(&yks_temp,buff,sizeof(rDevS_Yk));
				DevIdTemp=yks_temp.ParentId;
				IndexNoTemp=yks_temp.IndexNo;
			}
			break;
		case TABLE_RDEVS_YM:
			{
				memcpy(&yms_temp,buff,sizeof(rDevS_Ym));
				DevIdTemp=yms_temp.ParentId;
				IndexNoTemp=yms_temp.IndexNo;
			}
			break;
		}

		buff = NULL;
		buff_len = 0;

		Num = GetRecordBuffbyAppType_TableId_RecordId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, DevIdTemp%PARENT_OFFSET, buff, &buff_len);
		if (Num==1 && buff && buff_len>0)
		{
			memcpy(&devs_temp,buff,sizeof(rDevS_Info));
			TaskIdTemp=devs_temp.ParentId;

			TaskId=TaskIdTemp;
			DevId=DevIdTemp;
			IndexNo=IndexNoTemp;

			return 1;
		}
		else
		{
			return -3;
		}
	}
	else
	{
		return -2;
	}
}

//当前遥信是否使用在设备置检修上
bool DbCfgFun::CurYxIsUsedInDevRepair(unsigned int YxId)
{	
	if (YxId<=0)
	{
		return true;
	}

	char			sql[SQL_MAX_LEN];
	char			*buff = NULL;
	int			buff_len = 0;
	int			RetNum = 0;
	int			Count = 0;
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql, "select count(*) from rDevS_Info where devRepairYxId=%d", YxId+TABLE_RDEVS_YX*PARENT_OFFSET);
	RetNum = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (RetNum>0 && buff && buff_len>0)
	{
		memcpy(&Count, buff, sizeof(unsigned int));
	}

	if (buff)
	{
		delete [](char *)buff;
		buff = NULL;
	}

	if (Count >0)
	{
		return true;
	}

	return false;
}

//	当前Sub通道是否已经关联过主从通道
bool DbCfgFun::CurTaskIsSub(int TaskId)
{	
	if (TaskId<=0)
		return false;

	char			sql[SQL_MAX_LEN];
	char			*buff = NULL;
	int			buff_len = 0;
	int			RetNum = 0;
	int			Count = 0;
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql, "select count(*) from rTask_Info where RtCongenerId=%d", TaskId+TABLE_RTASK_INFO*PARENT_OFFSET);
	RetNum = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (RetNum>0 && buff && buff_len>0)
	{
		memcpy(&Count, buff, sizeof(int));
	}

	if (buff)
	{
		delete [](char *)buff;
		buff = NULL;
	}

	if (Count >0)
	{
		return false;
	}

	return true;
}

//	配置节点和通道的主备关联和主从状态
int DbCfgFun::ConfigConegerAndMasState(int AppType, int TableId, int RecordId, int RltRecordId, bool bConfigMaste)
{
	if (AppType<=0 || TableId<=0 || RecordId <=0 )
	{
		return -1;
	}

	char	 sql[SQL_MAX_LEN];
	QString	 SetValStr = 0;

	memset(sql, 0, SQL_MAX_LEN);
	if (SYSTEM_TYPE_NSA_YD == AppType && TABLE_RTASK_INFO == TableId)
	{
		sprintf(sql, "update rTask_Info set RtCongenerId=%d, RtConfigMaster=%d where ID=%d", RltRecordId, bConfigMaste, RecordId);
	}else
	{
		return -2;
	}
	return g_pSqLiteDB.DB_Modify(sql);
}

//	当前Sub通道是否已经关联过主从通道
bool DbCfgFun::CurSubTaskIsRelated(int TaskId)
{
	if (TaskId<=0)
		return true;

	char			sql[SQL_MAX_LEN];
	char			*buff = NULL;
	int			buff_len = 0;
	int			RetNum = 0;
	int			Count = 0;
	memset(sql, 0, SQL_MAX_LEN);
	sprintf(sql, "select count(*) from rTask_Info where RtCongenerId=%d", TaskId+TABLE_RTASK_INFO*PARENT_OFFSET);
	RetNum = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (RetNum>0 && buff && buff_len>0)
	{
		memcpy(&Count, buff, sizeof(int));
	}

	if (buff)
	{
		delete [](char *)buff;
		buff = NULL;
	}

	if (Count >0)
	{
		return true;
	}else
	{
		return false;
	}
}

//	配置遥控转发表关联闭锁遥信信息
int DbCfgFun::ConfigYkTransmitAndYx(int RecordId,unsigned int TaskIdYxLock, unsigned int DevIdYxLock, unsigned int IndexNoYxLock)
{
	if (RecordId <=0 )
	{
		return -1;
	}

	char	 sql[SQL_MAX_LEN];
	QString	 SetValStr = 0;

	memset(sql, 0, SQL_MAX_LEN);
		
	sprintf(sql, "update rTransmit_Yk set TaskIdYxLock=%d, DevIdYxLock=%d, IndexNoYxLock=%d where ID=%d", TaskIdYxLock, DevIdYxLock, IndexNoYxLock, RecordId);

	return g_pSqLiteDB.DB_Modify(sql);
}

//将Str的内容填充到BUFF
int DbCfgFun::InputStrToBuff(char *buff, QString Str, int BuffLen)
{
	if (!buff || BuffLen<=0 ||Str.length()<=0)
	{
		return -1;
	}

	memset(buff, 0, BuffLen);
	int RealLen = (Str.toLocal8Bit().length()<BuffLen ? Str.toLocal8Bit().length() : BuffLen);
	if (RealLen>0)
	{
		memcpy(buff, Str.toLocal8Bit().data(), RealLen);
	}
	buff[BuffLen-1] = 0;
	return RealLen;
}

//检查IP地址的正确性
bool DbCfgFun::CheckIpIsSafe(char IP[], int Length)
{
	if (!IP || Length<=0)
	{
		return false;
	}
	int Num = 0;
	for (int i=0; i<Length; i++)
	{
		if ('.' == IP[i])
		{
			Num++;
		}
	}
	if (3!= Num)
	{
		return false;
	}

	QString str;
	QString str1;
	str = QString::fromLocal8Bit(IP);
	if (str.contains(' ',Qt::CaseInsensitive))
	{
		return false;
	}
	QStringList ipv4 = str.split(QLatin1String("."));
	if (ipv4.count() != 4)
		return false;

	unsigned int	ipv4Address = 0;
	unsigned char	charValue = 0;
	bool ok = false;
	for (int i = 3; i>=0; i--)
	{
		ok = false;
		charValue = 0;
		str1 = ipv4[i];
		if (str1.at(0) == '0' && str1.length()>1)
		{
			return false;
		}
		charValue = ipv4[i].toUInt(&ok);
		if (!ok || 
			((0 >= charValue || charValue > 223) && 0 == i) ||
			(charValue > 255 && i>0))
		{
			return false;
		}
	}
	return true;
}

//	获取当前表的空闲点号（unsigned int类型，加1顺序递增）
int DbCfgFun::GetNewFreeIndexNo(int AppType, int TableId,unsigned int ParentId, unsigned short &IndexNo)
{
	IndexNo = 0;
	int Result = 0;
	if (SYSTEM_TYPE_NSA_YD != AppType || ParentId<=0)
	{
		return -2;
	}
	if (TABLE_RDEVS_YX != TableId && TABLE_RDEVS_RYB != TableId &&
		TABLE_RDEVS_YC != TableId && TABLE_RDEVS_BHYC != TableId &&
		TABLE_RDEVS_YK != TableId &&
		TABLE_RDEVS_YM != TableId)
	{
		return -1;
	}

	char	sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*buff = NULL;
	int	Name_len = 0;
	int	buff_len = 0;

	int	IndexNoNum = 0;
	bool	bGetNewId = false;
	memset(sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &Name_len);
	if (Result <=0 || !pEngName || Name_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}
		return -2;
	}

	sprintf(sql, "select IndexNo from %s where ParentId=%d order by IndexNo", pEngName,ParentId);
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	IndexNoNum = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (IndexNoNum>0 && buff && buff_len>0)
	{
		if (IndexNoNum*sizeof(unsigned short) != buff_len)
		{
			if (buff)
			{
				delete [](char*)buff;
				buff = NULL;
			}
			IndexNoNum = -1;
		}
	}else if (IndexNoNum == 0)
	{
		IndexNoNum = 1;
		IndexNo = 1;
	}

	if (buff && IndexNoNum>0)
	{
		if (((unsigned short*)buff)[0]>1)
		{
			IndexNo = 1;
		}else
		{
			bGetNewId = false;
			for (int i=0; i<IndexNoNum-1; i++)
			{
				if ( (((unsigned short*)buff)[i+1]-((unsigned short*)buff)[i]) > 1)
				{
					IndexNo = ((unsigned short*)buff)[i]+1;
					bGetNewId = true;
					break;
				}
			}

			if (!bGetNewId)
			{
				IndexNo = ((unsigned short*)buff)[IndexNoNum-1]+1;
			}
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	if (IndexNo>=0xFFFF)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("点号已超过最大值，表ID[%1], 应用类型[%1]").arg(TableId).arg(AppType));
		IndexNo = 0;
		return -3;
	}
	return IndexNoNum;
}

//	获取当前表的最大空闲点号（unsigned int类型，加1顺序递增）
int DbCfgFun::GetMaxFreeIndexNo(unsigned int AppType, unsigned int TableId,unsigned int ParentId,  unsigned short &IndexNo)
{
	IndexNo = 0;
	if (SYSTEM_TYPE_NSA_YD != AppType || ParentId<=0)
	{
		return -2;
	}
	if (TABLE_RDEVS_YX != TableId && TABLE_RDEVS_RYB != TableId &&
		TABLE_RDEVS_YC != TableId && TABLE_RDEVS_BHYC != TableId &&
		TABLE_RDEVS_YK != TableId &&
		TABLE_RDEVS_YM != TableId)
	{
		return -1;
	}

	char	sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*buff = NULL;
	int	Name_len = 0;
	int	buff_len = 0;

	int	Result = 0;
	memset(sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &Name_len);
	if (Result <=0 || !pEngName || Name_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}
		return -2;
	}

	sprintf(sql, "select max(IndexNo) from %s  where ParentId=%d ", pEngName,ParentId);
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}
	Result = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);
	if (Result>0 && buff && buff_len>0)
	{
		memcpy(&IndexNo, buff, sizeof(unsigned short));
		if (IndexNo<=0)
		{
			IndexNo = 1;
		}else
		{
			IndexNo+=1;
		}
	}else if (0 ==Result)
	{
		IndexNo = 1;
		Result = 1;
	}

	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	return Result;
}


int	DbCfgFun::GetInsertHeadSql(int AppType, int TableId, char *pTableName, QString &SqlStr)
{
	SqlStr.clear();
	if (AppType<=0 || TableId<=0 || !pTableName)
	{
		return -1;
	}
	if (QString(pTableName).length()>0)
	{
		//sprintf(Sql, "replace into %s values", TableName);
		//sprintf(Sql, "insert into %s values", pTableName);
		SqlStr = QString::fromLocal8Bit("insert into %1 values").arg(QString::fromLocal8Bit(pTableName));
		return 1;
	}else
	{
		return -2;
	}
	return 0;
}

int DbCfgFun::GetValuesSql(char Recorbuff[], int Buff_len, cColumn_Info *pColumnInfo, int *pMemPos, int ColumNum, int Type, QString &SqlStr)
{
	SqlStr.clear();
	if (!Recorbuff || Buff_len<=0 || !pColumnInfo || !pMemPos || ColumNum<=0)
		return -1;

	QString qStr;
	qStr.clear();
	QString valueStr;
	for(int i=0; i<ColumNum; i++)
	{
		if (pMemPos[i]+pColumnInfo[i].clmDataLength <= Buff_len)
		{
			valueStr.clear();	
			switch(pColumnInfo[i].clmDataType) 
			{
			case DATA_TYPE_BYTE:
				{
					char charVal = 0;
					memcpy(&charVal, Recorbuff + pMemPos[i], sizeof(char));
					valueStr.setNum(charVal);
				}
				break;
			case DATA_TYPE_UCHAR:
			case DATA_TYPE_UBYTE:
				{
					unsigned char ucharVal = 0;
					memcpy(&ucharVal, Recorbuff + pMemPos[i], sizeof(unsigned char));
					valueStr.setNum(ucharVal);
				}
				break;
			case DATA_TYPE_SHORT:
				{
					short ShortVal = 0;
					memcpy(&ShortVal, Recorbuff + pMemPos[i], sizeof(short));
					valueStr.setNum((short)ShortVal);
				}
				break;
			case DATA_TYPE_USHORT:
				{
					unsigned short uShortVal = 0;
					memcpy(&uShortVal, Recorbuff + pMemPos[i], sizeof(unsigned short));
					valueStr.setNum((ushort)uShortVal);
				}
				break;
			case DATA_TYPE_INT:
				{
					int intVal = 0;
					memcpy(&intVal, Recorbuff + pMemPos[i], sizeof(int));
					valueStr.setNum((int)intVal);
				}
				break;
			case DATA_TYPE_UINT:
				{
					unsigned int uintVal = 0;
					memcpy(&uintVal, Recorbuff + pMemPos[i], sizeof(unsigned int));
					valueStr.setNum((unsigned int)uintVal);
				}
				break;
			case DATA_TYPE_FLOAT:
				{
					float floatVal =0.0;
					memcpy(&floatVal, Recorbuff + pMemPos[i], sizeof(float));
					valueStr.setNum((float)floatVal);
				}
				break;
			case DATA_TYPE_DOUBLE:
				{
					double doubleVal = 0.0;
					memcpy(&doubleVal, Recorbuff + pMemPos[i], sizeof(double));
					valueStr.setNum((double)doubleVal);
				}
				break;
			case DATA_TYPE_LONGLONG:
				{
					qint64 LonglongVal = 0;
					memcpy(&LonglongVal, Recorbuff + pMemPos[i], sizeof(qint64));
					valueStr.setNum((qint64)LonglongVal);
				}
				break;
			case DATA_TYPE_CHAR:
			case DATA_TYPE_VARCHAR:
				{
					char *pChar;
					pChar = new char[pColumnInfo[i].clmDataLength+1];
					memset(pChar, 0, pColumnInfo[i].clmDataLength+1);
					memcpy(pChar, Recorbuff + pMemPos[i], pColumnInfo[i].clmDataLength);
					pChar[pColumnInfo[i].clmDataLength] = 0;
					valueStr = QString::fromLocal8Bit("'%1'").arg(QString::fromLocal8Bit(pChar));
					if(pChar)
					{
						delete [](char*)pChar;
						pChar = NULL;
					}
				}
				break;
			case DATA_TYPE_VARBINARY:
			default:
				break;
			}

			if(OPER_ROW_INSERT == Type)
			{
				qStr += valueStr;
				if(i<(ColumNum-1))
				{
					qStr += QString::fromLocal8Bit(",");			
				}
			}
		}
	}
	if (qStr.length() >= (SQL_MAX_LEN-100) )
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("Insert values语句太长!"));
		return -2;
	}
	SqlStr = QString::fromLocal8Bit("(") +qStr+ QString::fromLocal8Bit(")");

	return 1;
}


int DbCfgFun::GetPtlParamType(unsigned char RoutType,unsigned char Protocol)
{
	if (RoutType==ENUM_TASK_FOR_NSA)
	{
		if (Protocol==ENUM_PROTOCOL_NSA_TYPE_SERIAL_OLD_103 ||
			Protocol==ENUM_PROTOCOL_NSA_TYPE_NET_NEW_103  ||
			Protocol==ENUM_PROTOCOL_NSA_TYPE_NET_OLD_103 )
		{
			return ENUM_TASK_PARA_TYPE_103;
		}
		if (Protocol==ENUM_PROTOCOL_NSA_TYPE_61850)
		{
			return ENUM_TASK_PARA_TYPE_61850;
		}
	}
// 	if (RoutType==ENUM_TASK_FOR_SBC)
// 	{
// 		if (Protocol==ENUM_PROTOCOL_NSR_TYPE_103)
// 		{
// 			return ENUM_TASK_PARA_TYPE_103;
// 		}
// 	}
// 	if (RoutType==ENUM_TASK_FOR_PSL)
// 	{
// 		if (Protocol==ENUM_PROTOCOL_PSL_TYPE_SERIAL_103 ||
// 			Protocol==ENUM_PROTOCOL_PSL_TYPE_NET_103 )
// 		{
// 			return ENUM_TASK_PARA_TYPE_103;
// 		}
// 	}
// 	if (RoutType==ENUM_TASK_FOR_RCS)
// 	{
// 		if (Protocol==ENUM_PROTOCOL_RCS_TYPE_SERIAL_103 ||
// 			Protocol==ENUM_PROTOCOL_RCS_TYPE_NET_103 )
// 		{
// 			return ENUM_TASK_PARA_TYPE_103;
// 		}
// 	}
// 	if (RoutType==ENUM_TASK_FOR_CSC)
// 	{
// 		if (Protocol==ENUM_PROTOCOL_CSC_TYPE_SERIAL_103 ||
// 			Protocol==ENUM_PROTOCOL_CSC_TYPE_NET_103 )
// 		{
// 			return ENUM_TASK_PARA_TYPE_103;
// 		}
// 	}
// 	if (RoutType==ENUM_TASK_FOR_ISA)
// 	{
// 		if (Protocol==ENUM_PROTOCOL_ISA_TYPE_SERIAL_103 ||
// 			Protocol==ENUM_PROTOCOL_ISA_TYPE_NET_103 )
// 		{
// 			return ENUM_TASK_PARA_TYPE_103;
// 		}
// 	}
// 	if (RoutType==ENUM_TASK_FOR_OTHER)
// 	{
// 		if (Protocol==ENUM_PROTOCOL_OTHER_TYPE_MODIBUS)
// 		{
// 			return ENUM_TASK_PARA_TYPE_MODIBUS;
// 		}
// 		if (Protocol==ENUM_PROTOCOL_OTHER_TYPE_CDT)
// 		{
// 			return ENUM_TASK_PARA_TYPE_CDT;
// 		}
// 	}
	if (RoutType==ENUM_TASK_FOR_SCADA)
	{
		if (Protocol==ENUM_PROTOCOL_SCADA_TYPE_101)
		{
			return ENUM_TASK_PARA_TYPE_101;
		}else if (Protocol==ENUM_PROTOCOL_SCADA_TYPE_104 )
		{
			return ENUM_TASK_PARA_TYPE_104;
		}
	}

	return 0;
}

int DbCfgFun::AddTaskParaRecord(unsigned int TaskId, unsigned char RoutType,unsigned char Protocol)
{

	int Result = 0;
	rTask_Para_Info task_para_info_temp;
	int TaskParamType = 0;
	GetColumnInfo TempColumInfo;

	TaskParamType = GetPtlParamType(RoutType, Protocol);

	memset(&task_para_info_temp,0x0,sizeof(rTask_Para_Info));
	task_para_info_temp.ParentId=TaskId%PARENT_OFFSET+TABLE_RTASK_INFO*PARENT_OFFSET;

	
	TempColumInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_PARA_INFO,TaskParamType);

	SetDefaultData((char*)&task_para_info_temp, 
		TempColumInfo.m_pColumnInfo, 
		TempColumInfo.m_pDataOffset,
		TempColumInfo.m_columnNum, 
		TempColumInfo.m_RecordLength,
		TaskId%PARENT_OFFSET+TABLE_RTASK_INFO*PARENT_OFFSET);

	GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_PARA_INFO, task_para_info_temp.ID, 0);
	if (!TempColumInfo.IsSafe() || task_para_info_temp.ID<=0)
	{
		TempColumInfo.ReleasColumnInfo();
		return -1;
	}
		Result = InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_PARA_INFO, (char*)&task_para_info_temp, TempColumInfo.m_RecordLength, TempColumInfo.m_pColumnInfo, TempColumInfo.m_pDataOffset, TempColumInfo.m_columnNum);
		
		TempColumInfo.ReleasColumnInfo();
		return Result;
	

}


//	创建数据表
int DbCfgFun::CreateNewTable(int AppType, int TableId)
{
	if (AppType <=0 || TableId<=0)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN*5];
	int	Ret = 0;
	memset(Sql, 0, SQL_MAX_LEN*5);
	char TableName[TABLE_ENG_NAME_LEN];
	memset(TableName, 0, TABLE_ENG_NAME_LEN);
	Ret = GetTableEngNamebyAppType_TableId(AppType, TableId, TableName);
	if (Ret<= 0)
	{
		return -1;
	}
	if (CheckTableIsExist(TableName, TABLE_ENG_NAME_LEN))
	{
		DropTable(TableName,TABLE_ENG_NAME_LEN, AppType, TableId);
	}
	Ret = GetCreateTableSQL(AppType, TableId, Sql, TableName);
	if (Ret<=0)
	{
		return Ret;
	}
	qDebug()<<Sql;
	Ret = g_pSqLiteDB.DB_Modify(Sql); 
	if (Ret<=0)
	{
		return Ret;
	}

	return 1;
}


//检查表是否存在
int DbCfgFun::CheckTableIsExist(char TableName[], int bufflen)
{
	if (!TableName || bufflen<=0)
	{
		return -1;
	}

	QString Str = QString::fromLocal8Bit(TableName);
	if (Str.length()<=0)
	{
		return -1;
	}
	char			sql[SQL_MAX_LEN];
	char			*buff = NULL;
	int			buff_len = 0;
	int			RetNum = 0;
	int			Count = 0;
	memset(sql, 0, SQL_MAX_LEN);
	
	sprintf(sql, "select count(*) from sqlite_master where type='table' and name='%s'", TableName);
	RetNum = g_pSqLiteDB.DB_SelectUInt( sql, buff, &buff_len);
	if (RetNum>0 && buff && buff_len>0)
	{
		memcpy(&Count, buff, sizeof(int));
	}

	if (buff)
	{
	delete [](char *)buff;
	buff = NULL;
	}
	
	return Count;
}


//	删除表
int DbCfgFun::DropTable(char TableName[], int bufflen, int AppType, int TableId)
{
	if (!TableName || bufflen<=0 || AppType<=0 || TableId <= 0)
	{
		return -1;
	}

	QString Str = QString::fromLocal8Bit(TableName);
	if (Str.length()<=0)
	{
		return -1;
	}

	char	sql[SQL_MAX_LEN];
	memset(sql, 0, SQL_MAX_LEN);

	sprintf(sql, "drop table %s", TableName);
	return g_pSqLiteDB.DB_Modify(sql);
}


//	创建数据表
int DbCfgFun::CreateNewDevTable(SqLiteDB *pSqlDB, int AppType, int TableId, bool bDropOldTableFlag)
{
	if (AppType <=0 || TableId<=0 || !pSqlDB)
	{
		return -1;
	}

	char	Sql[SQL_MAX_LEN*5];
	int	Ret = 0;
	memset(Sql, 0, SQL_MAX_LEN*5);
	char TableName[TABLE_ENG_NAME_LEN];
	memset(TableName, 0, TABLE_ENG_NAME_LEN);
	Ret = GetTableEngNamebyAppType_TableId(AppType, TableId, TableName);
	if (Ret<= 0)
	{
		return -1;
	}
	if (CheckDevTableIsExist(pSqlDB, TableName, TABLE_ENG_NAME_LEN))
	{
		if (bDropOldTableFlag)
		{
			DropDevTable(pSqlDB,TableName);
		}
	}

	Ret = GetCreateTableSQL(AppType, TableId, Sql, TableName);
	if (Ret<=0)
	{
		return Ret;
	}
	qDebug()<<Sql;
	Ret = pSqlDB->DB_Modify(Sql); 
	if (Ret<=0)
	{
		return Ret;
	}

	return 1;
}


//检查表是否存在
int DbCfgFun::CheckDevTableIsExist(SqLiteDB *pSqlDB,char TableName[], int bufflen)
{
	if (!TableName || bufflen<=0 || !pSqlDB)
	{
		return -1;
	}

	QString Str = QString::fromLocal8Bit(TableName);
	if (Str.length()<=0)
	{
		return -1;
	}
	char		sql[SQL_MAX_LEN];
	char		*buff = NULL;
	int			buff_len = 0;
	int			RetNum = 0;
	int			Count = 0;
	memset(sql, 0, SQL_MAX_LEN);

	sprintf(sql, "select count(*) from sqlite_master where type='table' and name='%s'", TableName);
	RetNum = pSqlDB->DB_SelectUInt( sql, buff, &buff_len);
	if (RetNum>0 && buff && buff_len>0)
	{
		memcpy(&Count, buff, sizeof(int));
	}

	if (buff)
	{
		delete [](char *)buff;
		buff = NULL;
	}

	return Count;
}


//	删除表
int DbCfgFun::DropDevTable(SqLiteDB *pSqlDB,char TableName[])
{
	if (!TableName || !pSqlDB)
	{
		return -1;
	}

	QString Str = QString::fromLocal8Bit(TableName);
	if (Str.length()<=0)
	{
		return -1;
	}

	char	sql[SQL_MAX_LEN];
	memset(sql, 0, SQL_MAX_LEN);

	sprintf(sql, "drop table %s", TableName);
	return pSqlDB->DB_Modify(sql);
}


//	获取当前表的最大空闲ID（unsigned int类型，加1顺序递增）
int DbCfgFun::GetMaxFreeId_DeviceDB(SqLiteDB *pSqlDB,unsigned int AppType, unsigned int TableId, unsigned int &RecordId)
{
	RecordId = 0;
	if (AppType<=0 || TableId<=0 || !pSqlDB)
	{
		return -1;
	}
	char	sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*buff = NULL;
	int	Name_len = 0;
	int	buff_len = 0;
	//	int	RecordCount = 0;
	int	Result = 0;
	memset(sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &Name_len);
	if (Result <=0 || !pEngName || Name_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}
		return -2;
	}

	sprintf(sql, "select max(ID) from %s ", pEngName);
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}
	Result = pSqlDB->DB_SelectUInt(sql, buff, &buff_len);
	if (Result>0 && buff && buff_len>0)
	{
		memcpy(&RecordId, buff, sizeof(unsigned int));
		if (RecordId<=0)
		{
			RecordId = 1;
		}else
		{
			RecordId += 1;			
		}

	}else if (0 ==Result)
	{
		RecordId = 1;
		Result = 1;
	}

	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	return Result;
}


//	获取当前表的空闲ID（unsigned int类型，加1顺序递增）
int DbCfgFun::GetNewFreeId_DeviceDB(SqLiteDB *pSqlDB,int AppType, int TableId, unsigned int &RecordId)
{
	RecordId = 0;
	if (AppType<=0 || TableId<=0 || !pSqlDB)
	{
		return -1;
	}
	if (SYSTEM_TYPE_NSA_CD == AppType)
	{
		return -2;
	}

	int Result = 0;
	
	char	sql[SQL_MAX_LEN];
	char	*pEngName = NULL;
	char	*buff = NULL;
	int	Name_len = 0;
	int	buff_len = 0;
	int	IDNum = 0;
	bool	bGetNewId = false;
	memset(sql, 0, SQL_MAX_LEN);
	Result = GetTableEngNamebyAppType_TableId(AppType, TableId, pEngName, &Name_len);
	if (Result <=0 || !pEngName || Name_len <=0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}
		return -2;
	}

	sprintf(sql, "select ID from %s order by ID", pEngName);
	
	if (pEngName)
	{
		delete []pEngName;
		pEngName = NULL;
	}

	IDNum = pSqlDB->DB_SelectUInt(sql, buff, &buff_len);
	if (IDNum>0 && buff && buff_len>0)
	{
		if (IDNum*sizeof(unsigned int) != buff_len)
		{
			if (buff)
			{
				delete [](char*)buff;
				buff = NULL;
			}
			IDNum = -1;
		}
	}else if (IDNum == 0)
	{
		IDNum = 1;
		
		RecordId = 1;
	}

	if (buff && IDNum>0)
	{
	    if (((unsigned int*)buff)[0]>1)
		{
			RecordId = 1;
		}else
		{
			bGetNewId = false;
			for (int i=0; i<IDNum-1; i++)
			{
				if ( (((unsigned int*)buff)[i+1]-((unsigned int*)buff)[i]) > 1)
				{
					RecordId = ((unsigned int*)buff)[i]+1;
					bGetNewId = true;
					break;
				}
			}

			if (!bGetNewId)
			{
				RecordId = ((unsigned int*)buff)[IDNum-1]+1;
			}
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	if (RecordId>=PARENT_OFFSET)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("记录ID已超过最大ID限制，表ID[%1], 应用类型[%2]").arg(TableId).arg(AppType));
		RecordId = 0;
		return -3;
	}
	return IDNum;
}
